java - How to hide Android LinearLayout in onCreate? -
i've got android switch when turned on, expands linearlayout below it. use animation. when start screen button turned off, linearlayout shown. when turn switch on, , off again, indeed hides linearlayout, said, whenever screen starts, linearlayout shown default. know how can hide linearlayout default when screen starts?
the code have follows:
public class myclass extends activity implements oncheckedchangelistener { switch myswitch; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.my_layout); myswitch = (switch) findviewbyid(r.id.my_switch); myswitch.setoncheckedchangelistener(this); } @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked){ linearlayout view = (linearlayout) findviewbyid(r.id.view_to_expand); animation anim = expand(view, true); view.startanimation(anim); } else { linearlayout view = (linearlayout) findviewbyid(r.id.view_to_expand); animation anim = expand(view, false); view.startanimation(anim); } } public static animation expand(final view v, final boolean expand) { try { method m = v.getclass().getdeclaredmethod("onmeasure", int.class, int.class); m.setaccessible(true); m.invoke(v, measurespec.makemeasurespec(0, measurespec.unspecified), measurespec.makemeasurespec(((view) v.getparent()).getmeasuredwidth(), measurespec.at_most)); } catch (exception e) { e.printstacktrace(); } final int initialheight = v.getmeasuredheight(); if (expand) { v.getlayoutparams().height = 0; } else { v.getlayoutparams().height = initialheight; } v.setvisibility(view.visible); animation = new animation() { @override protected void applytransformation(float interpolatedtime, transformation t) { int newheight = 0; if (expand) { newheight = (int) (initialheight * interpolatedtime); } else { newheight = (int) (initialheight * (1 - interpolatedtime)); } v.getlayoutparams().height = newheight; v.requestlayout(); if (interpolatedtime == 1 && !expand) v.setvisibility(view.gone); } @override public boolean willchangebounds() { return true; } }; a.setduration(250); return a; } }
you can try use viewswitcher. add xml layout , add linearlayout in , empty layout well. on application start can start empty layout , switch other one
<viewswitcher xmlns:android = "http://schemas.android.com/apk/res/android" android:id="@+id/layoutswitcher" android:layout_width="fill_parent" android:layout_height="fill_parent" > <linearlayout></linearlayout> <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="6dip" > <imageview.... </<linearlayout> </viewswitcher>
then in code
switcher = (viewswitcher) findviewbyid(r.id.layoutswitcher); switcher.shownext();
Comments
Post a Comment