我有3个相对布局,可见性设置为GONE和3个按钮。当我按下一个按钮向我显示一个特定的布局,当我按下另一个按钮向我显示不同的布局并隐藏之前的布局时,我希望这样。
这是我的活动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>
答案 0 :(得分:1)
我根据你的格式编写代码,试试这个:
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);
}
});
}