Custom Android layout that handles its children

Posted by Gromix on Stack Overflow See other posts from Stack Overflow or by Gromix
Published on 2011-01-05T12:41:30Z Indexed on 2011/01/05 12:54 UTC
Read the original article Hit count: 195

Filed under:
|
|
|

Hi,

I'm trying to create a custom Android control to emulate a LinearLayout with a fancier display. Basically, I want the exact behaviour of a LinearLayout, but also borders, a background, ...

I could do it all in XML (works great) but since I have dozens of occurences in my app it's getting hard to maintain. I thought it would be nicer to have something like this:

/* Main.xml */
<MyFancyLayout>
    <TextView />
    <ImageView />
</MyfancyLayout>

My problem is, I don't want to have to re-write LinearLayout, so is there a way to only change its appearance? I got as far as this, which doesn't work... can anyone think of a better approach?

/* MyFancyLayout.xml */
<merge>
    ... the complex hierarchy to make it look like what I want
    ... with background attributes etc
</merge>    

and

/* MyFancyLayout.java */
public class MyFancyLayout extends LinearLayout
{
  // inflate the XML
  // move all the real children (as given by main.xml) to the inflated layout
  // do I still need to override onMeasure and onLayout?
}

Cheers!

Romain

© Stack Overflow or respective owner

Related posts about android

Related posts about custom