I am unable to override attributes when using <include> in my Android layout files. When I searched for bugs, I found Declined Issue 2863:
"include tag is broken (overriding layout params never works)"
Since Romain indicates this works in the test suites and his examples, I must be doing something wrong.
My project is organized like this:
res/layout
buttons.xml
res/layout-land
receipt.xml
res/layout-port
receipt.xml
The buttons.xml contains something like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button .../>
<Button .../>
</LinearLayout>
And the portrait and landscape receipt.xml files look something like:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
...
<!-- Overridden attributes never work. Nor do attributes like
the red background, which is specified here. -->
<include
android:id="@+id/buttons_override"
android:background="#ff0000"
android:layout_width="fill_parent"
layout="@layout/buttons"/>
</LinearLayout>
What am I missing?
I just found the issue. First, you can only override layout_* attributes, so the background won't work. That is documented behavior and simply an oversight on my part.
The real problem is found in LayoutInflater.java:
If the <include> tag does not include both layout_width and layout_height, the RuntimeException occurs and is silently handled, without any log statement even.
The solution is to always include both layout_width and layout_height when using the <include> tag, if you want to override any of the layout_* attributes.
My example should change to:
I submitted an enhancement request to allow all included attributes to be overridden:
I found I sometimes miss including the android:id tag when using the GUI builder in Eclipse. Making sure (when I notice) that I add into a TextView from the builder , the id I'm using in the ListView layout.
becomes
Instead of getting 'false' 'false' I get :) and includes working ok.