Access to the included XML layout

I need to access the view presented in a.xml layout included in another b.xml layout. For example, This is a.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/xyz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="XYZ" />
</RelativeLayout>

And, in b.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <include
        android:id="@+id/a_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        layout="@layout/a" />

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/xyz"
        android:text="Show me below xyz" />
</RelativeLayout>

I need to do this in xml code, because if I do this in Java, it should be after setContentView (), and then setting LayoutParams from the TextView label will not work.

I think everyone understands what I'm trying to ask. Waiting for good answers.

Thanks to everyone.

The image on the right is what I am trying to achieve, and left one thing - this is what I get with the current code.

This is what I am getting with the current xml codeThis is what I am trying to do

+1
source share
2 answers

In b.xml you should fix:

android:layout_below="@id/xyz"

to

android:layout_below="@id/a_layout"

And then you can use it in your code (here I put it in onCreate):

setContentView(R.layout.b);    
((Button)findViewById(R.id.xyz)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            ((TextView)findViewById(R.id.label)).setText("clicked on XYZ button");
        }
    });
+3
source

View , , "". : ​​a.xml, b.xml , b.xml a.xml, Android (?). , , , android:layout_below="@id/a_layout", @Hoàng Toản.

P.S. a + b:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/xyz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="XYZ" />
    </RelativeLayout>

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/xyz"
        android:text="Show me below xyz" />
</RelativeLayout>
0

All Articles