Android 中布局的显示隐藏切换按钮
Show-hide toggle button for Layout in Android
我想知道为什么我对 show/hide 我的布局所做的按钮只工作一次,即最初我的布局是 GONE,然后当我点击一个按钮时它们是可见的,但后来当我点击同一个按钮,他们的 View 没有被设置回 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);
}
}
这是一个适合您的示例
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>
我想知道为什么我对 show/hide 我的布局所做的按钮只工作一次,即最初我的布局是 GONE,然后当我点击一个按钮时它们是可见的,但后来当我点击同一个按钮,他们的 View 没有被设置回 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);
}
}
这是一个适合您的示例
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>