簡體   English   中英

如何在onCreate中隱藏Android LinearLayout?

[英]How to hide Android LinearLayout in onCreate?

我有一個Android開關,該開關打開時會在其下方展開LinearLayout。 為此,我使用動畫。 當我啟動屏幕時,該按鈕已關閉,但顯示了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布局中,並在其中添加LinearLayout和一個空布局。 在您的應用程序啟動時,您可以從空白布局開始,然后切換到另一個布局

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

然后在代碼中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM