在Android中为布局显示隐藏切换按钮 [英] Show-hide toggle button for Layout in Android

查看:104
本文介绍了在Android中为布局显示隐藏切换按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我制作的用于显示/隐藏我的布局的按钮只能使用一次,即最初我的布局已消失,然后当我单击一个按钮时它们是可见的,但是后来当我单击同一按钮时,它们视图未重新设置为消失。

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


推荐答案

此处是一个适合您的示例

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

这里是布局

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

这篇关于在Android中为布局显示隐藏切换按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆