android - How to instantiate custom view when other arguments are added? -
i know simple answer, i'm little stumped here. i'm creating custom view, panel, following constructor:
public panel(context context, attributeset attrs, int barlength){ super(context, attrs); //paint object drawing in ondraw barpaint = new paint(); bar = new rect(3, 13, barlength, 3); // rect object //...other stuff, etc etc } in activity view used, created 5 different times within different cells of table layout. panel simple rect length vary based on argument barlength.
so, in activity panel views created, they're called so:
private panel tagbar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tagbar = (panel) findviewbyid(r.id.custview); } as can tell, doesn't use constructor outlined above. how instantiate new panel, including passing right context , attributeset? seems when constructed (without int barlength argument) passed in automatically. how should tagbar instantiated pass 3 arguments?
to clarify, attrs.xml styleable attributes file used set colors of bars xml. can give me direction?
thanks!
if have panel class in xml, framework calls proper constructor you, passing context , attributes. can create new attribute in attrs.xml, , once that's done can pass bar length via xml well
<resources> <declare-styleable name="panel"> <attr name="barlength" format="int" /> </declare-styleable> </resources> then in xml add barlength=n, other attribute in xml, without android: in front of it.
Comments
Post a Comment