这是一个适合您的示例
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>