Let’s say I want a custom view with 2 checkboxes. I would start to create a layout file like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
</LinearLayout>
Then create a View that extends FrameLayout, adding the layout using the layout-inflator:
mInflate.inflate(R.layout.foo, this, true);
My problem with this approach is that I am nesting 2 layouts just to have a very basic view with 2 checkboxes. As I am currently working on a very complex layout, I am seeking for a way to avoid unnecessary nesting of layouts when using the layout inflator.
You could use the
mergetag in your xml layout and then inflate it:FrameLayoutprobably isn’t the best solution for your layout because it will stack the views one on top of the other.