如何在按钮单击时显示/隐藏布局 [英] how to show/hide layout on button click

查看:93
本文介绍了如何在按钮单击时显示/隐藏布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在第一个相对布局中有两个相对布局,在第二个相对布局中有列表..,我想在开始时只有一个带有按钮的布局会在屏幕上显示一个按钮,当我单击按钮然后进行布局时列表视图从右侧打开,顶部带有新按钮,prevoius按钮隐藏.屏幕分为两个具有不同布局的部分.

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


                        }
                    });

推荐答案

尝试一下:

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

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.

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

希望这会有所帮助.

这篇关于如何在按钮单击时显示/隐藏布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆