如何在android中更改relativelayout的位置/视图

时间:2015-05-31 06:40:12

标签: android relativelayout

我的布局有3 RelativeLayout个,如下所示。

  • RelativeLayout1   - >其他小部件和Switch
  • RelativeLayout2   - > 2 ImageButton垂直放置
  • RelativeLayout3   - > 2 Button垂直放置

默认情况下,Switch的状态处于关闭状态。

我想要的是

  1. Switch处于关闭状态时,应放置RelativeLayout3或应与RelativeLayout2重叠,即第3布局应位于第2位
  2. 当启用Switch时(即处于开启状态),Relativelayout2应该是可见的,而Relativelayout3应该低于它。
  3. 以下是我的布局

    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bckgrnd_4">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Task Name"
            android:id="@+id/textView5"
            android:layout_alignBottom="@+id/editText"
            android:layout_alignStart="@+id/textView6" />
    
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:hint="@string/hint_name"
            android:layout_marginTop="37dp"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/textView7" />
    
         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set"
            android:id="@+id/button5"
            style="@style/btnStyleBeige"
            android:layout_marginTop="37dp"
            android:layout_below="@+id/textView5"
            android:layout_alignStart="@+id/switch1" />
    
       <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Set Location"
            android:id="@+id/textView6"
            android:layout_alignBottom="@+id/button5"
            android:layout_toStartOf="@+id/editText" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Always alert me"
            android:id="@+id/textView7"
            android:layout_centerVertical="true"
            android:layout_alignParentStart="true" />
    
        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/btnStyleBeige"
            android:id="@+id/switch1"
            android:layout_alignTop="@+id/textView7"
            android:layout_alignEnd="@+id/editText" />
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/relative2"
            android:layout_marginTop="37dp"
            android:layout_below="@+id/switch1"
            android:layout_alignParentStart="true">
    
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageButton3"
                android:src="@drawable/cal"
                style="@style/btnStyleBeige"
                android:layout_marginStart="42dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true" />
    
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageButton4"
                style="@style/btnStyleBeige"
                android:src="@drawable/alert"
                android:layout_marginStart="67dp"
                android:layout_alignBottom="@+id/imageButton3"
                android:layout_toEndOf="@+id/imageButton3" />
    
        </RelativeLayout>
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/relative3"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true">
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Add Task"
                style="@style/btnStyleBeige"
                android:id="@+id/button6" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Cancel"
                style="@style/btnStyleBeige"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/button6"
                android:layout_marginStart="37dp" />
    
        </RelativeLayout>
    
    </RelativeLayout>
    

    请帮帮我......

1 个答案:

答案 0 :(得分:0)

您可以为Switch设置一个监听器,并根据Switch是打开还是关闭来切换布局的可见性。例如,

    Switch s = (Switch) findViewById(R.id.switch1);

    s.setOnCheckedChangedListener (this);

    ...
}

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    RelativeLayout relative2 = (RelativeLayout) findViewById(R.id.relative2);
    relative2.setVisibility(isChecked ? View.GONE:View.VISIBLE);

}