The Relative Layout is very flexible layout used in android for custom layout designing. It gives us the flexibility to position our component/view based on the relative or sibling component’s position. Just because it allows us to position the component anywhere we want so it is considered as most flexible layout. For the same reason Relative layout is the most used layout after the 线性布局 in Android. It allow its child view to position relative to each other or relative to the container or another container.
In Relative Layout, you can use “above, below, left and right” to arrange the component’s position in relation to other component. For example, in the below image you can see content is placed in related to Heading.
Table Of Contents
Lets see different properties of Relative Layout which will be used while designing Android App UI:
1.above: Position the bottom edge of the view above the given anchor view ID and must be a reference of the another resource in the form of id. Example, android:layout_above=”@+id/textView” .
For example, suppose a view with id textview2 is what we want to place above another view with id textview. Below is the code and layout image.
<文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:textAppearance=“?android:attr/textAppearanceLarge” android:text="Text2" android:id=“@+id/textView2” android:layout_above="@+id/textView" android:layout_marginBottom="100dp" android:layout_centerHorizontal="true"/>
In the below example we have aligned a view with id textView2 Bottom of another view with id textView. Below is the coded and layout image.
<文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:textAppearance=“?android:attr/textAppearanceLarge” android:layout\u centerHorizontal=“真” android:id=“@+id/textView2” android:layout_alignBottom="@+id/textView" android:text="Text2 alignBottom of Text1" android:layout\u marginBottom=“90dp” />
下面是代码和布局图像,其中我们将id为textView2的视图与id为textView的另一个视图左对齐。
<!-- 文本视图2对齐文本视图的左侧--> <文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:textAppearance=“?android:attr/textAppearanceLarge” android:id=“@+id/textView2” android:layout\u alignLeft=“@+id/文本视图” android:text=“Text2对齐Text1的左侧” android:layout\u below=“@+id/文本视图” android:layout\u marginTop=“20dp”/>
下面是代码和布局图像,其中我们将id为textView2的视图与id为textView的另一个视图的右侧对齐。
<!-- 文本视图2对齐文本视图的右侧--> <文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:textAppearance=“?android:attr/textAppearanceLarge” android:id=“@+id/textView2” android:layout\u alignRight=“@+id/文本视图” android:text=“Text2对齐Text1的右侧” android:layout\u below=“@+id/文本视图” android:layout\u marginTop=“20dp”/>
下面是alignStart代码和布局图像,其中我们将id为textView2的视图与id为textView的另一个视图的开始对齐。
<!-- Text2 alignStart--> <文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:textAppearance=“?android:attr/textAppearanceLarge” android:id=“@+id/textView2” android:text=“Text2对齐Text1的开头” android:layout\u alignStart=“@+id/文本视图” />
下面是alignTop代码和布局图像,其中我们将id为的视图与id为的另一个图像的文本视图顶部对齐 imageView.
<文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:textAppearance=“?android:attr/textAppearanceLarge” android:id=“@+id/textView” android:layout\u alignTop=“@+id/图像视图” android:text="Text Here is AlignTop on Image" />
重要提示:alignParentBottom and alignBottom are two different properties. In alignBottom we give the reference of another view in the form of id that the view is aligned at the bottom of referenced view but in alignParentBottom the bottom edge of the view matches the bottom edge of the parent.
Below is the alignParentBottom code and layout image in which textView is simply displayed using the alignParentBottom.
<文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:textAppearance=“?android:attr/textAppearanceLarge” android:id=“@+id/textView” android:text="Text Here is AlignParentBottom with bottom margin of 120dp" android:layout_alignParentBottom="true" android:layout\u alignParentLeft=“true” android:layout\u alignParentStart=“true” android:layout_marginBottom="120dp" />
重要提示: In alignParentEnd the bottom edge of the view matches the bottom edge of the parent.
Below is the alignParentEnd code and layout image in which textView is simply displayed on Image in the end.
<文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:textAppearance=“?android:attr/textAppearanceLarge” android:id=“@+id/textView” android:text="Text in Parent End" android:layout\u alignBottom=“@+id/图像视图” android:layout_alignParentEnd="true" />
重要提示: alignParentLeft and alignLeft are two different properties. In alignLeft we give the reference of another view in the form of id that the view is aligned to the left of the referenced view but in alignParentLeft the left edge of the view matches the left edge of the parent.
下面是alignParentLeft示例代码和布局图像,其中textView仅显示在左侧的父图像上。
<!-- 在Android中左对齐父对象--> <文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:textAppearance=“?android:attr/textAppearanceLarge” android:id=“@+id/textView” android:text=“父级中的文本左侧” android:layout\u alignBottom=“@+id/图像视图” android:layout\u alignParentLeft=“true” />
重要提示: alignParentRight和alignRight是两个不同的属性。在alignRight中,我们以id的形式给出另一个视图的引用,该视图与引用视图的右侧对齐,但在alignParentRight中,视图的右边缘与父视图的右边缘匹配。
下面是alignParentRight示例代码和布局图像,其中textView仅显示在右侧的父图像上。
<!-- alignRightParent示例--> <文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:textAppearance=“?android:attr/textAppearanceLarge” android:id=“@+id/textView” android:text=“父权限中的文本” android:layout\u alignBottom=“@+id/图像视图” android:layout\u alignParentRight=“true” />
重要提示: alignParentStart和alignStart是两个不同的属性,在alignStart中,我们以id的形式提供另一个视图的引用,该视图在引用视图的开始处对齐,但在alignParentStart中,视图的开始边缘与父视图的开始边缘匹配(RelativeLayout).
下面是alignParentStart示例代码和布局图像,其中textView仅显示在右侧的父图像上。
<!-- alignParentStart示例--> <文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:textAppearance=“?android:attr/textAppearanceLarge” android:id=“@+id/textView” android:text=“父级开始中的文本” android:layout\u alignTop=“@+id/图像视图” android:layout\u alignParentStart=“true” />
重要提示: alignParentTop和alignTop是两个不同的属性,在alignTop中,我们以id的形式提供另一个视图的引用,该视图与被引用视图的顶部对齐,但在alignParentTop中,视图的顶边与父视图的顶边匹配(RelativeLayout).
下面是alignParentTop属性和布局图像的示例代码。
<!-- alignParentTop示例--> <文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:text=“Text1对齐父顶部” android:layout\u alignParentTop=“true” android:layout\u margin=“20dp” android:textSize=“20sp” android:textColor=“#000”/>
下面是centerInParent属性和布局图像的示例代码。
<!-- centerInParent示例--> <文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:text=“父级中的Text1中心” android:layout\u centerInParent=“true” android:textSize=“20sp” android:textColor=“#000” />
下面是centerHorizontal属性和布局图像的示例代码。
<!-- 中心水平示例--> <文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:text=“Text1水平居中” android:layout\u centerHorizontal=“真” android:textSize=“20sp” android:textColor=“#000” />
下面是centerVertical属性和布局图像的示例代码。
<!-- 中心垂直示例--> <文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:text=“Text1中心垂直” android:layout\u centerVertical=“真” android:textSize=“20sp” android:textColor=“#000” />
示例1: 在这里,我们正在设计一个简单的登录屏幕 Android Studio 使用相对布局。以下是最终输出:
<?xml版本=“1.0”编码=“utf-8”?> <!--使用相对布局--> <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android" android:layout\u width=“匹配父项” android:layout\u height=“match\u parent”> <!--用于在UI顶部显示登录文本的文本视图--> <文本视图 android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:textAppearance=“?android:attr/textAppearanceLarge” android:text=“登录” android:id=“@+id/textView3” android:layout\u alignParentTop=“true” android:layout\u centerHorizontal=“真”/> <!--显示用户名的文本视图--> <文本视图 android:id=“@+id/用户名” android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:layout\u marginLeft=“@dimen/activity\u horizontal\u margin” android:layout\u marginTop=“110dp” android:text=“用户名:” android:textColor=“#000000” android:textSize=“20sp”/> <!--显示密码的文本视图--> <文本视图 android:id=“@+id/密码” android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:layout\u below=“@+id/用户名” android:layout\u margin=“@尺寸/活动\u水平\u margin” android:text=“密码:” android:textColor=“#000000” android:textSize=“20sp”/> <!--编辑用于填充用户名的文本--> <编辑文本 android:id=“@+id/edt\u用户名” android:layout\u width=“填充父对象” android:layout\u height=“40dp” android:layout\u marginLeft=“@dimen/activity\u horizontal\u margin” android:layout\u marginTop=“100dp” android:layout\u toRightOf=“@+id/用户名” android:hint=“用户名”/> <!--编辑用于填写密码的文本--> <编辑文本 android:layout\u width=“填充父对象” android:layout\u height=“40dp” android:layout\u below=“@+id/edt\u用户名” android:layout\u centerVertical=“真” android:layout\u toRightOf=“@+id/密码” android:提示=“密码”/> <!--填写详细信息后单击的按钮--> <按钮 android:id=“@+id/btnLogin” android:layout\u width=“包装内容” android:layout\u height=“包装内容” android:layout\u below=“@+id/密码” android:layout\u centerHorizontal=“真” android:layout\u marginTop=“20dp” android:背景=“#03B424” android:text=“登录” android:textColor=“#ffffff” android:textStyle=“bold”/> </RelativeLayout>
输出:
相对布局:
线性布局:
android:orientation=“水平”或android:orientation=“垂直”
美好的
无法理解alignParentStart和类似的其他参数。。