在RelativeLayout(相对布局)中,每个组件都可以通过ID来指定相对于其它组件或者父组件的位置。
比如以下的layout_relative01.xml文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" ></Button> <Button android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button01" android:layout_toRightOf="@id/button01" android:text="按钮2" ></Button> </RelativeLayout>
比如以下的layout_relative02.xml文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="按钮1" ></Button> <Button android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button01" android:layout_alignLeft="@id/button01" android:text="按钮2" ></Button> </RelativeLayout>
比如以下的layout_relative03.xml文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="按钮1" ></Button> <Button android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button01" android:layout_alignParentLeft="true" android:text="按钮2" ></Button> <Button android:id="@+id/button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="按钮3" ></Button> </RelativeLayout>
比如以下的layout_relative04.xml文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="按钮1" ></Button> <Button android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="按钮2" ></Button> <Button android:id="@+id/button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="按钮3" ></Button> </RelativeLayout>
5、在相对布局中,如果想固定一个组件的位置,至少要确定组件的”左右”和”上下”两个位置,才可以准确地固定组件位置。
下面的layout_relative05.xml文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" ></Button> <Button android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/button01" android:text="按钮2" ></Button> <Button android:id="@+id/button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button01" android:text="按钮3" ></Button> </RelativeLayout>
这时,我们想在按钮3的右边,按钮2的下方,放置一个按钮4,有以下的layout_relative06.xml文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" ></Button> <Button android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/button01" android:text="按钮2" ></Button> <Button android:id="@+id/button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button01" android:text="按钮3" ></Button> <Button android:id="@+id/button04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/button03" android:text="按钮4" ></Button> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" ></Button> <Button android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/button01" android:text="按钮2" ></Button> <Button android:id="@+id/button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button01" android:text="按钮3" ></Button> <Button android:id="@+id/button04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/button03" android:layout_below="@id/button02" android:text="按钮4" ></Button> </RelativeLayout>
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/231797.html原文链接:https://javaforall.net
