how to show/hide layout on button click

2020-03-26 03:09发布

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


                        }
                    });

4条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-03-26 03:34
 button.setOnClickListener(this);
    public void onClick(View v) {
    layoutid.setVisibility(View.GONE);
    }
查看更多
ら.Afraid
3楼-- · 2020-03-26 03:35

You can try with isShown() as suggested by Amiya

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

   // Your_Staff
    }
    else{
          // Your_Staff
    }
}
});
</b>
查看更多
混吃等死
4楼-- · 2020-03-26 03:47

Set visibility as Gone instead of Invisible

查看更多
不美不萌又怎样
5楼-- · 2020-03-26 03:48

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.

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.

查看更多
登录 后发表回答