I tried it well, and realized that it did not work. The problem is that there are no methods in the View class to add child views. Child views should only be added to ViewGroups. Layouts, such as LinearLayoutexpand ViewGroup. Therefore, instead of expanding the View you need to extend, for example LinearLayout.
Then in your XML, link to the layout using:
<my.package.MyView
android:id="@+id/CompId"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
:
public class MyView extends LinearLayout {
public MyView(Context context) {
super(context);
this.initComponent(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
this.initComponent(context);
}
private void initComponent(Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.foobar, null, false);
this.addView(v);
}
}