Creating a custom component based on LinearLayout, declaring a layout in XML

I am trying to create Compound Control in Android 1.5 (as described here ), but havn't been able to find any good examples of how to do this, using an XML file to specify the layout. I am fine with creating an Activity and then loading an xml file using the following in the constructor:

setContentView(R.layout.main);

However, I want to do this in a subclass of LinearLayout - so I can use this composite component in other XML layouts. Sort of:

public class CustomView extends LinearLayout
{
  public CustomView(Context context) {
       super(context);
       setupView();
  }
  public CustomView(Context context, AttributeSet attrs)
  {
      super(context, attrs);
      setupView();
  }
  public void setupView()
  {
    setContentView(R.layout.custom); // Not possible
  }
}

What is the right way to do this?

+5
source share
1 answer

"" :

LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.custom, this, true);
+13

All Articles