I have an XML layout similar to this (the XML and code is a lot more complex but I’ve simplified for brevity):
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/button01"
android:src="@drawable/key_01" />
<ImageButton
android:id="@+id/button02"
android:src="@drawable/key_02" />
<ImageButton
android:id="@+id/button03"
android:src="@drawable/key_03" />
</TableRow>
</TableLayout>
</merge>
I have this layout as a custom widget.
public class ThreeButtons extends LinearLayout
{
public ThreeButtons(Context context, AttributeSet attrs)
{
super(context, attrs);
final LayoutInflater inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflator.inflate(R.layout.three_buttons, this);
if (!isInEditMode())
setClickListeners();
}
...
private void setClickListeners()
{
...
}
}
The issue is that I have an extra level of depth that I don’t think should be there.
ie: the actual ThreeButtons class is a LinearLayout that only has a single child, which is the inflated XML.
I would have thought that it would make more sense for the widget to be created directly from the XML rather than appending it as a child. Is the way I’m doing it the only way to have custom widgets? Or is there a cleaner way?
Thanks,
Brad
Rather than extending a LinearLayout, you can have your widget extend TableLayout.
Then, within your Merge tag, simply contain the list of rows that you want inflated into your widget.
This eliminates the additional LinearLayout.
Example: