Search code examples
androidviewgrouplayout-inflaterandroid-inflate

Layout parameters don't apply when inflating content from layout XML on custom ViewGroup


I'm using the LayoutInflater to add a layout into a custom ViewGroup (not a subclass of ...Layout but a simple ViewGroup).

After a short battle with it, and after implementing all that was said in this post, I managed to make the content to finally appear, but for some reason it's not laid out as I want it to. In the layout XML there are 3 elements that need to distribute horizontally, with the central element "pushing" the ones on the edges.

Here's the code that's inflating the layout:

private LayoutInflater inf = null;
private Paint backpaint = null;
private ViewGroup inflated = null;

private void inflate(Context context) {
    this.setBackgroundColor(0x00000000);
    inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflated = (ViewGroup)inf.inflate(R.layout.application_heading, null);
    this.addView(inflated, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    inflated.measure(r, b);
    inflated.layout(l, t, r, b);
}

At first I only added the inflated.layout(l, t, r, b) but it didn't really work. After I've added the inflated.measure(r,b), I finally saw the content, but it was all stuck together, as if the LinearLayout that was the base for the inflated XML was set to WRAP_CONTENT.

I tried to run through children of the inflated ViewGroup and call .layout(l,t,r,b) on them, but that made them stretch to fill the entire layout, one on top of the other.

I must be missing something really small here. Anyone knows what it might be?


Solution

  • Switched the containing class to subclass a RelativeLayout instead of ViewGroup and everything became peachy.