0

我想知道为什么我用来显示/隐藏我的布局的按钮只能工作一次,即最初我的布局已经消失,然后当我单击一个按钮时它们是可见的,但后来当我单击同一个按钮时,它们的视图不是被设置回 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);
    }
}
4

1 回答 1

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>
于 2016-08-25T06:38:28.423 回答