Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android include tag not overriding layout attributes

I am having trouble using <include> in conjunction with <merge>. I am developing on Eclipse with the latest Android SDK (4.3). Here is my sample code:

test_merge.xml

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

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFF0000" >
    </View>

</merge>

test_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include layout="@layout/test_merge" 
        android:layout_width="100dp" 
        android:layout_height="100dp"/>

</LinearLayout>

MyActivity.java

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_main);
    }
}

The problem is that I should see a red square on the top left corner, 100x100dp. Instead, the whole screen is red. Thus, for some reason the android:layout_width and android:layout_height attributes in the <include> have no effect.

I have read the documentation and it said that in order to override attributes via the <include> tag, I must override android:layout_width and android:layout_height, which I have done.

What am I doing wrong?

like image 950
user2566395 Avatar asked Feb 06 '26 17:02

user2566395


1 Answers

When using merge, there is no parent to apply 100dps to. Try switching to FrameLayout instead of merge or (even better) in this case you may just remove merge and have View be the root.

Edit:

Change test_merge.xml to:

<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFFF0000" >
</View>
like image 167
MaciejGórski Avatar answered Feb 09 '26 10:02

MaciejGórski