Does the Android XML layout โ€œenableโ€ tag really work?

I cannot override attributes when using <include> in my Android layout files. When I searched for errors, I found Declined Issue 2863 :

"enable tag (violating layout settings never works)

Since Romain points out that this works in test suites and examples, I should be doing something wrong.

My project is organized as follows:

res/layout buttons.xml res/layout-land receipt.xml res/layout-port receipt.xml 

There is something like this in the buttons.xml file:

 <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> 

Both the portrait and landscape receipt.xml files look something like this:

 <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?

+80
android android widget
Apr 13 '10 at 17:10
source share
3 answers

I just found a problem. First, you can only override the layout_ * attributes, so the background will not work. This is documented behavior and just control on my part.

The real problem is in LayoutInflater.java:

 // We try to load the layout params set in the <include /> tag. If // they don't exist, we will rely on the layout params set in the // included XML file. // During a layoutparams generation, a runtime exception is thrown // if either layout_width or layout_height is missing. We catch // this exception and set localParams accordingly: true means we // successfully loaded layout params from the <include /> tag, // false means we need to rely on the included layout params. ViewGroup.LayoutParams params = null; try { params = group.generateLayoutParams(attrs); } catch (RuntimeException e) { params = group.generateLayoutParams(childAttrs); } finally { if (params != null) { view.setLayoutParams(params); } } 

If the <include> tag does not include both layout_width and layout_height, a RuntimeException occurs and is silently processed without any logging.

The solution is to always include both layout_width and layout_height when using the <include> tag if you want to override any layout_ * attributes.

My example should change to:

 <include android:id="@+id/buttons_override" android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/buttons"/> 
+129
Apr 13 '10 at
source share

I submitted an improvement request to allow overriding of all included attributes:

Suppose I have two identical layouts other than the values โ€‹โ€‹of a TextView . Currently, I have either modified the layout at runtime or duplicated XML.

For example, to pass two parameters with the values โ€‹โ€‹"hello" and "world" to layout1:

<include layout="@layout/layout1a" params="textView=hello|editText=world" />

layout1a.xml:

<merge><TextView text="@param/textView"><EditText hint="@param/editText"></merge>

An alternative implementation will break encapsulation and allow the include statement to override values โ€‹โ€‹such as:

<include layout="@layout/layout1b" overrides="@id/textView.text=hello|@id/editText.hint=world" />

layout1b.xml:

<merge><TextView id="@+id/textView"><EditText hint="@+id/editText"></merge>

+10
Sep 30 '12 at 15:35
source share

I found that sometimes I skip including the android: id tag when using the GUI builder in Eclipse. Making sure (when I notice) that I am adding to the TextView from the builder, the identifier that I use in the ListView layout.

 <TextView android:text="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> ... 

becomes

 <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> ... 

Instead of "false" false, I get :) and includes working in order.

+2
Oct 29 2018-11-11T00:
source share



All Articles