在onClick上切换布局的可见性

Lam*_*one 4 java layout android

当我点击一个按钮时,我需要设置这个布局,我的java就像:

Layout propLayout = (Layout) findViewById(R.id.properLayout);

    public void propsBtn(View view) {
propLayout.setVisiblity(View.Visible);
}
Run Code Online (Sandbox Code Playgroud)

我知道布局线我完全错了!如果有人能告诉我如何正确设置,我将非常感激:)

这是我的XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mainBackGround"
tools:context="com.myapplication2.app.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp">

...contents...

    </RelativeLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"
    android:id="@+id/properLayout">

    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_gravity="bottom"
        android:padding="8dp">

...contents...           

    </RelativeLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Lam*_*one 6

DerGolem给出了正确的答案,但他删除了它,所以我现在在这里报告:

//First we set visibility value of interested layout either to gone, visible or invisible
android:visibility="gone"
Run Code Online (Sandbox Code Playgroud)

然后在onCreate下写如下:

//specify the button that has to handle visibility  
ImageButton properties = (ImageButton) findViewById(R.id.propBtn);

//And the layout we want to change is visibility
final LinearLayout propLayout = (LinearLayout) findViewById(R.id.properLayout);

            properties.setOnClickListener
            (
                    new View.OnClickListener()
                    {
                        public void onClick(View v)
                        {
                            if (propLayout.getVisibility() == View.VISIBLE)
                            {
                                propLayout.setVisibility(View.INVISIBLE);
                            }
                            else
                            {
                                propLayout.setVisibility(View.VISIBLE);
                            }
                        }
                    }
            );
Run Code Online (Sandbox Code Playgroud)