【问题标题】:How to hide and show multiple layouts?如何隐藏和显示多个布局?
【发布时间】:2015-10-25 22:57:54
【问题描述】:

我有 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>

【问题讨论】:

    标签: android android-layout onclicklistener visibility android-button


    【解决方案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);
                  }
                });
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-07
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      相关资源
      最近更新 更多