Show-hide toggle button for Layout in Android

末鹿安然 提交于 2019-12-23 04:52:23

问题


I was wondering why a button I made to show/hide my layouts only works once, i.e, initially my layouts are GONE, then when I click a button they're VISIBLE, but later when I click the same button, their View is not being set back to 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:


Here is a sample which should work for you

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

Here is the layout

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


来源:https://stackoverflow.com/questions/39136540/show-hide-toggle-button-for-layout-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!