Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在onCreate中隐藏Android LinearLayout?_Java_Android_Hide - Fatal编程技术网

Java 如何在onCreate中隐藏Android LinearLayout?

Java 如何在onCreate中隐藏Android LinearLayout?,java,android,hide,Java,Android,Hide,我有一个安卓开关,当它打开时,会在下面展开一个线性布局。为此,我使用动画。当我启动屏幕时,按钮关闭,但会显示LinearLayout。当我现在将开关打开,然后再次关闭时,它确实隐藏了LinearLayout,但正如我所说,每当屏幕启动时,默认情况下都会显示LinearLayout。有人知道我如何在屏幕启动时默认隐藏LinearLayout吗 我现时的守则如下: public class MyClass extends Activity implements OnCheckedChangeList

我有一个安卓开关,当它打开时,会在下面展开一个线性布局。为此,我使用动画。当我启动屏幕时,按钮关闭,但会显示LinearLayout。当我现在将开关打开,然后再次关闭时,它确实隐藏了LinearLayout,但正如我所说,每当屏幕启动时,默认情况下都会显示LinearLayout。有人知道我如何在屏幕启动时默认隐藏LinearLayout吗

我现时的守则如下:

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;
    }
}

您可以尝试使用ViewSwitcher。将其添加到xml布局中,并在其中添加线性布局以及空布局。在应用程序启动时,可以从空布局开始,然后切换到另一个布局

<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>

请显示布局XML。LL最初的可见性状态是什么?为什么不在XML中指定您的
layout\u height
为0,而
visibility
gone
switcher = (ViewSwitcher) findViewById(R.id.layoutSwitcher);
switcher.showNext();