简体   繁体   English

如何在onCreate中隐藏Android LinearLayout?

[英]How to hide Android LinearLayout in onCreate?

I've got an Android Switch which when turned on, expands a LinearLayout below it. 我有一个Android开关,该开关打开时会在其下方展开LinearLayout。 For this I use an animation. 为此,我使用动画。 When I start the screen the button is turned off, but the LinearLayout is shown. 当我启动屏幕时,该按钮已关闭,但显示了LinearLayout。 When I now turn the Switch to on, and then back off again, it indeed hides the LinearLayout, but like I said, whenever the screen starts, the LinearLayout is shown by default. 现在,当我打开“开关”,然后再次将其关闭时,它确实隐藏了LinearLayout,但是就像我说的,每当屏幕启动时,默认情况下都会显示LinearLayout。 Does anybody know how I can hide the LinearLayout by default when the screen starts? 有人知道在屏幕启动时默认情况下如何隐藏LinearLayout吗?

The code I have now is as 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 a = 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 to use ViewSwitcher. 您可以尝试使用ViewSwitcher。 add it to an xml layout and add your LinearLayout in it and an empty layout as well. 将其添加到xml布局中,并在其中添加LinearLayout和一个空布局。 on your application start you can start with the empty layout and then switch to the 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 the code 然后在代码中

switcher = (ViewSwitcher) findViewById(R.id.layoutSwitcher);
switcher.showNext();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM