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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24523110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:21:07  来源:igfitidea点击:

how to show/hide layout on button click

androidandroid-layout

提问by user3683036

i want to have two relativelayout in first relativelayout have map and in second relativelayout i have the list..,i want on starting only layout with map will be visible on screen with a button,,when i click on button then layout with listview get open from right side with new new button on the top of it,,and prevoius button get hide.and screen get divided with two parts with different layouts..i have done some thing but from starting onward m getting half half screen.

我想在第一个相对布局中有两个相对布局有地图,在第二个相对布局中我有列表..,我想在开始时只有地图布局将通过按钮在屏幕上可见,当我单击按钮时,使用列表视图进行布局得到从右侧打开,顶部有新按钮,前一个按钮隐藏。屏幕被分成具有不同布局的两个部分。

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

MainActivity

主要活动

 show = (TextView)findViewById(R.id.getdirection);
         show1 = (TextView)findViewById(R.id.hide);
         rt = (RelativeLayout)findViewById(R.id.rl_ListView2);
show.setOnClickListener(new OnClickListener() {

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

                            if(rt.getVisibility()==View.INVISIBLE)
                            {
                                rt.setVisibility(View.VISIBLE);

                            }
                           show.setVisibility(View.INVISIBLE);


                        }
                    });
show1.setOnClickListener(new OnClickListener() {

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

                            if(rt.getVisibility()==View.VISIBLE)
                            {
                                rt.setVisibility(View.INVISIBLE);

                            }
                           show1.setVisibility(View.INVISIBLE);


                        }
                    });

回答by MysticMagic?

Try this:

尝试这个:

You are getting layout1 in half screen from starting due to weight property. You can try not giving weight in starting and give it programatically on button click.

由于重量属性,您从一开始就在半屏中获得 layout1。您可以尝试在开始时不给予权重,并在单击按钮时以编程方式给予权重。

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);
    }
});

Hope this helps.

希望这可以帮助。

回答by Giant

 button.setOnClickListener(this);
    public void onClick(View v) {
    layoutid.setVisibility(View.GONE);
    }

回答by Forest baba

You can try with isShown()as suggested by Amiya

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

<b>
button2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
   if(button1.isShown()) {

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

回答by Richierich

From my view you did not set the orientation in the LinearLayoutto be vertical for the two RelativeLayouts. I also suggests you put the map and Buttonin a FrameLayoutas well instead of RelativeLayout.

在我看来,您没有LinearLayout将两个 RelativeLayouts的方向设置为垂直。我还建议您将地图和Button放在 a 中FrameLayout,而不是RelativeLayout.

回答by qulfille

Set visibility as Goneinstead of Invisible

将可见性设置为Gone而不是Invisible