PersonComposite extends LinearLayout
the default behavior was that my java class now had one child, the LinearLayout that is the root of the XML. This meant that each use of <my.app.PersonComposite ...> in other view XML would never apply configuration to the Linearlayout that has any effect.Wow. Even I don't understand that. Let's try this way. The LayoutInflater, actually created the following structure (details omitted for brevity):
So any attempts to use the component like this would never change the orientation of the two nested TextView Components because the configuration was applied to the outer LinearLayout:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout >
<LinearLayout >
<TextView .../>
<TextView .../>
</LinearLayout>
</LinearLayout>
Solve this problem by using <merge> as the root element. And make sure you invoke LayoutInflater:<my.app.PersonComposite
android:id="@+id/person"
android:orientation="vertical"
/>
Now the person_composite.xml looks something like this:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = inflater.inflate(R.layout.person_composite, this);
You can see how this works by getting the android source from git and looking at the source code for the<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView .../>
<TextView .../>
</merge>
LayoutInflater.inflate
method. then search that same source tree for an xml that uses <merge> as the root element. You'll find more than one.
No comments:
Post a Comment