Android 如何在单击按钮时显示/隐藏布局

Android 如何在单击按钮时显示/隐藏布局,android,android-layout,Android,Android Layout,我想在第一个relativelayout中有两个relativelayout有地图,在第二个relativelayout中我有列表..,我想在开始时,只有带有地图的布局才能在屏幕上用按钮显示,当我单击按钮时,然后从右侧打开带有listview的布局,顶部有新按钮,,和prevoius按钮被隐藏。屏幕被分成两部分,不同的布局。我做了一些事情,但从一开始我得到了半个屏幕 <LinearLayout xmlns:android="http://schemas.android.com/apk/re

我想在第一个relativelayout中有两个relativelayout有地图,在第二个relativelayout中我有列表..,我想在开始时,只有带有地图的布局才能在屏幕上用按钮显示,当我单击按钮时,然后从右侧打开带有listview的布局,顶部有新按钮,,和prevoius按钮被隐藏。屏幕被分成两部分,不同的布局。我做了一些事情,但从一开始我得到了半个屏幕

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ListView_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1" >

    <RelativeLayout
        android:id="@+id/rl_ListView1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5" >

        <Button
            android:id="@+id/getdirection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="Get Directions" />

        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.MapFragment" >
        </fragment>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_ListView2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:visibility="invisible" >

        <Button
            android:id="@+id/hide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="Get Directions" />

        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:visibility="invisible" />
    </RelativeLayout>

</LinearLayout>

将可见性设置为“已消失”,而不是“不可见”

尝试以下操作:

由于权重属性,您从开始时就得到了半屏幕布局1。你们可以试着在开始时不给重量,然后按程序点击按钮给重量

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        button1.setVisibility(View.GONE);  //hide old button
        layout2.setVisibility(View.VISIBLE);  //show layout2
        //set Relativelayout 1 to half screen
        RelativaLayout.LayoutParams params = layout1.getLayoutParams(); 
        params.weight = 0.5;
        layout1.setLayoutParams(params);
    }
});

希望这有帮助。

您可以按照Amiya的建议尝试使用
isShown()

 button.setOnClickListener(this);
    public void onClick(View v) {
    layoutid.setVisibility(View.GONE);
    }
<b>
button2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
   if(button1.isShown()) {

   // Your_Staff
    }
    else{
          // Your_Staff
    }
}
});
</b>

button2.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
如果(按钮1.isShown()){
//你的工作人员
}
否则{
//你的工作人员
}
}
});

在我看来,您没有将
线性布局中的方向设置为两个相对长度的垂直方向。我还建议您将地图和
按钮
放在
框架布局
中,而不是
相对布局

在按钮上设置布局的可见性单击可访问尝试使用“可见性消失”而不是“不可见”。@PurpleDroid我的问题是“活动开始”,只有地图在屏幕和按钮上可见。。,点击按钮,屏幕被分成两部分。1 wll显示地图和其他将显示列表。我不知道为什么,但参数。权重重新调整我的错误。不将权重作为属性param@user3683036请尝试LinearLayout.LayoutParams而不是RelativeLayout.LayoutParams。这对我没有帮助@PurpleDroid您想要实现什么以及正在发生的事情我无法理解您上面的问题@Hak Hak我的问题在活动开始时只有地图会显示在屏幕上并显示一个按钮。。,然后点击按钮屏幕被分成2个半部分。1个wll显示地图,另一个将显示列表。现在开始,我在点击按钮时得到我想要的分割屏幕
<b>
button2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
   if(button1.isShown()) {

   // Your_Staff
    }
    else{
          // Your_Staff
    }
}
});
</b>