android - How do I create child views with elements inside? -
i came across this video google demonstrated animated transitions creation of child views using onclick
add or remove child view. part of learning process: took code, broke down, , made changes (i not copy , paste programmer).
my question is: how add textview
, button
, other various elements child view when created?
(project download google if you're interested: full project)
here have in activity:
/** * custom view painted random background color , 2 different sizes * toggled between due user interaction. */ public class coloredview extends view { private boolean mexpanded = false; private linearlayout.layoutparams mcompressedparams = new linearlayout.layoutparams( viewgroup.layoutparams.match_parent, 50); private linearlayout.layoutparams mexpandedparams = new linearlayout.layoutparams( viewgroup.layoutparams.match_parent, 200); private coloredview(context context) { super(context); int red = (int)(math.random() * 128 + 127); int green = (int)(math.random() * 128 + 127); int blue = (int)(math.random() * 128 + 127); int color = 0xff << 24 | (red << 16) | (green << 8) | blue; setbackgroundcolor(color); setlayoutparams(mcompressedparams); setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // size changes cause layouttransition animation if changing transition enabled setlayoutparams(mexpanded ? mcompressedparams : mexpandedparams); mexpanded = !mexpanded; requestlayout(); } }); } }
i have in oncreate
method showing animations
layouttransition transition = eventcontainer.getlayouttransition(); transition.enabletransitiontype(layouttransition.changing);
this xml has linearlayout
in child views created in:
<linearlayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:animatelayoutchanges="true" android:id="@+id/eventcontainer"> </linearlayout>
by adding button or edittext linearlayout treats entirely new view, allowing 1 object per. there way slam bunch of objects 1 layout?
i'm little confused on concept of getting context
of views , layouts... misused somewhere in here.
you can't, @ least not in current code.
your coloredview
class extends view
, not support having own child views. if want add child views it, have create class extending viewgroup
(or of viewgroup's subclasses).
once class extends viewgroup
, can simple use addview()
method add views, , align them passing layoutparams
(you can create custom layoutparams
if view requires complex positioning).
Comments
Post a Comment