如何更改 android 中的 relativelayout position/view
How to change the position/view of relativelayout in android
我有一个布局,其中有 3 个 RelativeLayout
,它们的位置如下。
- RelativeLayout1
-> 其他小部件和
Switch
- RelativeLayout2
-> 2
ImageButton
s 垂直放置
- RelativeLayout3
-> 2
Button
s 垂直放置
默认情况下 Switch
的状态为关闭状态。
我要的是
- 当
Switch
处于关闭状态时,RelativeLayout3应该放置或重叠RelativeLayout2,即第三个布局应该出现在位置2
- 当
Switch
启用时(即处于打开状态),Relativelayout2 应该可见,Relativelayout3 应该在它下面。
下面是我的布局
<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>
请帮帮我...
您可以为 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);
}
我有一个布局,其中有 3 个 RelativeLayout
,它们的位置如下。
- RelativeLayout1
-> 其他小部件和
Switch
- RelativeLayout2
-> 2
ImageButton
s 垂直放置 - RelativeLayout3
-> 2
Button
s 垂直放置
默认情况下 Switch
的状态为关闭状态。
我要的是
- 当
Switch
处于关闭状态时,RelativeLayout3应该放置或重叠RelativeLayout2,即第三个布局应该出现在位置2 - 当
Switch
启用时(即处于打开状态),Relativelayout2 应该可见,Relativelayout3 应该在它下面。
下面是我的布局
<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>
请帮帮我...
您可以为 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);
}