When using merge as the root element of the layout of your Compound View, tools:parentTag can help you utilise all the features of the Design tool.
Most of the times, when creating a Compound View you want to inflate the layout xml directly into the created
ViewGroup. Without introducing any additional layouts in-between. That’s why you should be really familiar with how
merge tag works in layout xmls. Or at least what’s (probably) the most typical use-case of this tag.
The easiest way to understand it, would be:
- When creating a Compound View:
- When the
By default your layout design tool will not know what is the eventual type your
merge tag becomes. This can be a bit annoying if:
a) You like to use the design tool to set some of your
b) You like to peek at the Design visualisation to see if what you’re doing in xml is correct.
Here’s where the
parentTag parameter (of the
tools namespace) comes in handy. It provides you with a way of telling the Design tool, what will be the eventual
ViewGroup of the
(You will also need to add
android:layout_height to your
merge tag for the Design tool to work properly).
For the layout:
the design tool looks like this:
It is not aware that the
merge tag represents a
ConstraintLayout, therefore it cannot take into account any of the enclosed
ImageView’s layout parameters (and as a result cannot display anything).
If you add the
parentTag parameter to the layout (+
The design tool will understand that the merge tag really is a
ConstraintLayout, and it will display the enclosed elements correspondingly:
Remember that it doesn’t have to be exactly
ConstraintLayout. It can be any other
ViewGroup (including any compound view of yours that will extend some