如何隐藏和显示多个布局?
How to hide and show multiple layouts?
我有 3 个可见性设置为 GONE 的相关布局和 3 个按钮。我希望当我按下一个按钮显示一个特定的布局时,当我按下另一个按钮显示一个不同的布局并隐藏以前的布局时。
这是我的 activity xml:
<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=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
<RelativeLayout
android:id="@+id/rel"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text1"
android:text="Text 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
<RelativeLayout
android:id="@+id/rel2"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text2"
android:text="Text 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
<RelativeLayout
android:id="@+id/rel3"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text3"
android:text="Text 3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
我已经按照你的格式格式化了代码,试试这个:
RelativeLayout layout1,layout2,layout3;
Button button1,button2,button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout1=(RelativeLayout) findViewById(R.id.rel);
layout2=(RelativeLayout) findViewById(R.id.rel2);
layout3=(RelativeLayout) findViewById(R.id.rel3);
button1=(Button) findViewById(R.id.btn);
button2=(Button) findViewById(R.id.btn2);
button3=(Button) findViewById(R.id.btn3);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
layout1.setVisibility(View.VISIBLE);
layout2.setVisibility(View.GONE);
layout3.setVisibility(View.GONE);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.VISIBLE);
layout3.setVisibility(View.GONE);
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.GONE);
layout3.setVisibility(View.VISIBLE);
}
});
}
我有 3 个可见性设置为 GONE 的相关布局和 3 个按钮。我希望当我按下一个按钮显示一个特定的布局时,当我按下另一个按钮显示一个不同的布局并隐藏以前的布局时。
这是我的 activity xml:
<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=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
<RelativeLayout
android:id="@+id/rel"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text1"
android:text="Text 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
<RelativeLayout
android:id="@+id/rel2"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text2"
android:text="Text 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
<RelativeLayout
android:id="@+id/rel3"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text3"
android:text="Text 3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
我已经按照你的格式格式化了代码,试试这个:
RelativeLayout layout1,layout2,layout3;
Button button1,button2,button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout1=(RelativeLayout) findViewById(R.id.rel);
layout2=(RelativeLayout) findViewById(R.id.rel2);
layout3=(RelativeLayout) findViewById(R.id.rel3);
button1=(Button) findViewById(R.id.btn);
button2=(Button) findViewById(R.id.btn2);
button3=(Button) findViewById(R.id.btn3);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
layout1.setVisibility(View.VISIBLE);
layout2.setVisibility(View.GONE);
layout3.setVisibility(View.GONE);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.VISIBLE);
layout3.setVisibility(View.GONE);
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.GONE);
layout3.setVisibility(View.VISIBLE);
}
});
}