Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<merge/> in custom View xml layout

I have the following custom view which is based on RelativeLayout:

public class LearningModeRadioButton extends
                                        RelativeLayout
                                     implements
                                        Checkable,
                                        View.OnClickListener {

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.rb_learning_mode, this, true);
    }
}

R.layout.rb_learning_mode contents are:

<?xml version="1.0" encoding="utf-8"?>

<merge
        xmlns:android="http://schemas.android.com/apk/res/android"
        >

    <RadioButton
            android:id="@+id/rb"
            android:button="@drawable/sel_rb_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

    <TextView
            android:id="@+id/tv_mode_title"
            style="@style/text_regular"
            android:layout_toRightOf="@+id/rb"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    <TextView
            android:id="@+id/tv_description"
            style="@style/text_small"
            android:layout_below="@+id/tv_mode_title"
            android:layout_alignLeft="@+id/tv_mode_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

</merge>

It sort of works, but layout parameters (layout_xxx) are ignored. I could use another <RelativeLayout/> as root element of the layout but I want to avoid having extra level in view hierarchy.

So the question is: How do I make layout attributes inside <merge/> work?

like image 226
Andrii Chernenko Avatar asked May 29 '13 11:05

Andrii Chernenko


People also ask

What is use of merge in Android XML?

Merge Tag. The <merge /> tag helps us to eliminate redundant view groups in our view hierarchy when including one layout within another.

What is XML merge?

The XML Merge component is a transformation component used to take incoming data from upstream SSIS source components and merge them into one SSIS column data based on the XML data structure defined in the component. This data can be then consumed by a downstream pipeline component.

What does the tag in an XML layout file do?

The <include> tag lets you to divide your layout into multiple files: it helps dealing with complex or overlong user interface.

What is layout in XML?

Android Layout is used to define the user interface that holds the UI controls or widgets that will appear on the screen of an android application or activity screen. Generally, every application is a combination of View and ViewGroup.


2 Answers

For anyone that might still be struggling with this, you should specify the parentTag attribute inside the merge tag. You will also need to specify layout_height and layout_width to make it work.

    <merge
        tools:parentTag="android.widget.RelativeLayout"
        tools:layout_width="match_parent"
        tools:layout_height="match_parent"
    >
    // Child Views
    </merge>

The editor should display everything properly now.

like image 163
anishbajpai014 Avatar answered Nov 15 '22 13:11

anishbajpai014


Merge useful for LinearLayout and FrameLayout its not suitable for RelativeLayout.

Obviously, using works in this case because the parent of an activity's content view is always a FrameLayout. You could not apply this trick if your layout was using a LinearLayout as its root tag for instance. The can be useful in other situations though.

check this:

like image 23
Padma Kumar Avatar answered Nov 15 '22 15:11

Padma Kumar