Android中的布局显示隐藏切换按钮

时间:2016-08-25 04:05:31

标签: android android-layout togglebutton android-togglebutton

我想知道为什么我用来显示/隐藏我的布局的按钮只能工作一次,也就是说,最初我的布局是GONE,然后当我点击一个按钮时它们是可见的,但后来当我点击相同的按钮时,他们的视图未被设置回GONE。

    /**
 * Method to show/hide buttons, on button click.
 * @param v
 */
public void hideOrDisplayOptionIconsButton(View v)
{
    // Hide layouts if VISIBLE
    if(mMapViewsButtonsLinearLayout.getVisibility() == View.VISIBLE
       && mLocationButtonsLinearLayout.getVisibility() == View.VISIBLE)
    {
        mMapViewsButtonsLinearLayout.setVisibility(View.GONE);
        mLocationButtonsLinearLayout.setVisibility(View.GONE);
    }
    // Show layouts if they're not VISIBLE
    else
    {
        mMapViewsButtonsLinearLayout.setVisibility(View.VISIBLE);
        mLocationButtonsLinearLayout.setVisibility(View.VISIBLE);
    }
}

1 个答案:

答案 0 :(得分:1)

以下是适合您的示例

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    private LinearLayout mMapViewsButtonsLinearLayout=null;
    private LinearLayout mLocationButtonsLinearLayout=null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMapViewsButtonsLinearLayout= (LinearLayout) findViewById(R.id.mMapViewsButtonsLinearLayout);
        mLocationButtonsLinearLayout= (LinearLayout) findViewById(R.id.mLocationButtonsLinearLayout);

    }

    public void hideOrDisplayOptionIconsButton(View v)
    {
        // Hide layouts if VISIBLE
        if(mMapViewsButtonsLinearLayout.getVisibility() == View.VISIBLE
                && mLocationButtonsLinearLayout.getVisibility() == View.VISIBLE)
        {
            mMapViewsButtonsLinearLayout.setVisibility(View.GONE);
            mLocationButtonsLinearLayout.setVisibility(View.GONE);
        }
        // Show layouts if they're not VISIBLE
        else
        {
            mMapViewsButtonsLinearLayout.setVisibility(View.VISIBLE);
            mLocationButtonsLinearLayout.setVisibility(View.VISIBLE);
        }
    }
}

这是布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.hideshow.MainActivity">


    <LinearLayout
        android:id="@+id/mMapViewsButtonsLinearLayout"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button2" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/mLocationButtonsLinearLayout"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button4" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hide or Show"
            android:id="@+id/hideorshow"
            android:onClick="hideOrDisplayOptionIconsButton" />
    </LinearLayout>
</LinearLayout>