RelativeLayout的基本对齐方式

RelativeLayout的基本对齐方式在 RelativeLayo 相对布局 中 每个组件都可以通过 ID 来指定相对于其它组件或者父组件的位置 1 通过 ID 来指定相对于其它组件的位置 组件之间的相对位置关系设置如下 android layout above 将组件放在指定 ID 组件的上方 android layout below 将组件放在指定 ID 组件的下方 android layout toRig

在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

(0)
上一篇 2025年11月20日 下午8:01
下一篇 2025年11月20日 下午8:22


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号