简体   繁体   中英

Android: creating two linearlayout in single relative layout with on click button

In my single screen android project(using only one XML file) when i press button1 then linerlayout1 is open and when i press button2 then linearlayout2 is open.my button1 and button2 is placed in linearlayout3.can it works??if yes then how?? thanks in advance.

       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/relativeLayout1"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" >

       <LinearLayout
       android:id="@+id/linearLayout1"
       android:layout_width="fill_parent"
       android:layout_height="50dp"
       android:layout_above="@+id/linearLayout3"
       android:layout_alignParentLeft="true"
       android:layout_alignParentRight="true"
       android:layout_alignParentTop="true"
       android:orientation="vertical" >

      <RadioButton
      android:id="@+id/radioButton3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/linearLayout1" />

     <RadioButton
      android:id="@+id/radioButton4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/linearLayout1" />



     </LinearLayout>

     <LinearLayout
     android:id="@+id/linearLayout3"
     android:layout_width="fill_parent"
     android:layout_height="50dp"
     android:layout_alignParentBottom="true"
     android:layout_alignParentRight="true" >

    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Button1" />

    <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Button2" />


   </LinearLayout>

   <LinearLayout
   android:id="@+id/linearLayout2"
   android:layout_width="fill_parent"
   android:layout_height="50dp"
   android:layout_alignParentBottom="true"
   android:layout_alignParentLeft="true"
   android:orientation="vertical" >

    <RadioButton
    android:id="@+id/radioButton13"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/linearLayout2" />

    <RadioButton
    android:id="@+id/radioButton14"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/linearLayout2" />


        </LinearLayout>

        </RelativeLayout>

Yes, it's very simple:

Use the android:Visibility XML tag initially, and when each respective button is pressed, you'd obtain your reference to the LinearLayout s, and set their visibility to VISIBLE or GONE in code.

Note: you want to use GONE rather than INVISIBILE as INVISIBILE will still take up screen space, so there'll be a large chunk of blank space where it was before, whereas I assume you want them in place of each other

in the layout xml of linear layout 1 and 2, use

android:visibility="gone"

From code

LinearLayout l1 = (LinearLayout) findViewById(R.id.linearLayout2);

you can use the following to make it visible

l1.setVisibility(View.VISIBLE);

and to hide it

l1. setVisibility(View.GONE):

You can play around with the "setVisibility" property of your layout. In the "onCreate()" method you can do something like this

LinearLayout layout1 = (LinearLayout) view.findViewById(R.id.linearLayout1);
layout1.setVisibility(View.GONE);

And then on your button press

layout1.setVisibility(View.VISIBLE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM