Android基本布局-RelativeLayout
16lz
2021-12-04
相对布局可以让空间以相对位置的形式出现在布局的任意位置
控件与父布局对齐
<?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/button_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Button 1" /> <Button android:id="@+id/button_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="Button 2" /> <Button android:id="@+id/button_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Button 3" /> <Button android:id="@+id/button_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:text="Button 4" /> <Button android:id="@+id/button_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="Button 5" />RelativeLayout>
五个按钮分别出现在左上,右上,中间和左下,右下
注意到
android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_alignParentRight="true"android:layout_centerInParent="true"android:layout_alignParentBottom="true"
表示按钮分别与父布局的左,上,右,中,下对齐
控件与控件对齐
<?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/button_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Button 1" /> <Button android:id="@+id/button_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button_1" android:layout_toLeftOf="@+id/button_1" android:text="Button 2" /> <Button android:id="@+id/button_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button_1" android:layout_toRightOf="@id/button_1" android:text="Button 3" /> <Button android:id="@+id/button_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button_1" android:layout_toLeftOf="@id/button_1" android:text="Button 4" /> <Button android:id="@+id/button_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button_1" android:layout_toRightOf="@id/button_1" android:text="Button 5" />RelativeLayout>
Button2到4分别位于Button1的左上,右上,左下,右下
注意到
android:layout_centerInParent="true"android:layout_above="@+id/button_1"android:layout_toLeftOf="@+id/button_1"android:layout_toRightOf="@id/button_1"android:layout_below="@id/button_1"
button_1与父布局对齐居中显示,其他button分别与button_1对齐位于上,左,右,下
更多相关文章
- Android(安卓)ImageView ScaleType 图解
- android 入门xml布局文件
- android 入门xml布局文件
- 今天开始记录自学android的历程
- Android(安卓)Studio Android(安卓)UI控件学习笔记
- Android(安卓)选择控件
- Android超炫日期日历控件:TimesSquare
- 从零开始学android-一行两个按钮居中 布局
- 【4.2】Android_Edittext/Button