Android开发学习教程(14)- Android布局之相对布局RelativeLayout

0 691

—— 珍贵的东西往往数目稀少,所以你要变得更强,才有力量去抢。

上一篇我们讲了线性布局LinearLayout的基本用法,这里来学习常用布局之相对布局RelativeLayout的基本用法。

相对布局是什么

相对布局按照控件间的相对位置排列,比如控件A相对于控件B在控件B的左边,并且和控件B顶部对齐等。

相对布局有什么用

通过相对位置来控制控件在屏幕的位置。

相对布局怎么用

继续基于上一篇的项目,我们新建一个RelativeLayoutActivity:

1
2
3
4
5
6
7
8
9
10
11
12
13
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" encoding="utf-8"?>
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:background="#F0F0F0"
        android:gravity="center"
        android:text="我是第一个子控件" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tv1"
        android:layout_toLeftOf="@+id/tv1"
        android:text="我是第二个子控件" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tv1"
        android:layout_toRightOf="@+id/tv1"
        android:text="我是第三个子控件" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tv1"
        android:layout_toLeftOf="@+id/tv1"
        android:text="我是第四个子控件" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tv1"
        android:layout_toRightOf="@+id/tv1"
        android:text="我是第五个子控件" />
</RelativeLayout>

Android开发学习教程(14)- Android布局之相对布局RelativeLayout

第一个控件

1
2
3
4
5
6
7
8
<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    android:background="#F0F0F0"
    android:gravity="center"
    android:text="我是第一个子控件" />
1
2
3
android:layout_height="100dp":表示TextView控件高度为100dp
android:layout_centerInParent="true":表示TextView控件位于父控件RelativeLayout的水平居中并且垂直居中
android:gravity="center":表示TextView控件上面显示的内容水平居中并且垂直居中

第二个控件

1
2
3
4
5
6
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/tv1"
    android:layout_toLeftOf="@+id/tv1"
    android:text="我是第二个子控件" />
1
2
android:layout_alignTop="@+id/tv1":表示与id为tv1控件顶部对齐
android:layout_toLeftOf="@+id/tv1":表示在id为tv1控件的左边

第三个控件

1
2
3
4
5
6
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/tv1"
    android:layout_toRightOf="@+id/tv1"
    android:text="我是第三个子控件" />
1
2
android:layout_alignTop ="@+id/tv1"表示与id为tv1控件顶部对齐
android:layout_toRightOf ="@+id/tv1"表示在id为tv1控件的右边

第四个控件

1
2
3
4
5
6
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/tv1"
    android:layout_toLeftOf="@+id/tv1"
    android:text="我是第四个子控件" />
1
2
android:layout_alignBottom="@+id/tv1"表示与id为tv1控件底部对齐
android:layout_toLeftOf="@+id/tv1"表示在id为tv1控件的左边

第五个控件

1
2
3
4
5
6
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/tv1"
    android:layout_toRightOf="@+id/tv1"
    android:text="我是第五个子控件" />
1
2
android:layout_alignBottom="@+id/tv1"表示与id为tv1控件底部对齐
android:layout_toRightOf="@+id/tv1"表示在id为tv1控件的右边

RelativeLayout常用基本属性

第一类属性 属性值为true或者false

1
2
3
4
5
6
7
8
9
10
11
12
13
android:layout_centerHrizontal:水平居中
android:layout_centerVertical:垂直居中
android:layout_centerInparent:相对于父控件完全居中
android:layout_alignParentBottom:贴紧父控件的下边缘
android:layout_alignParentLeft:贴紧父控件的左边缘
android:layout_alignParentRight:贴紧父控件的右边缘
android:layout_alignParentTop:贴紧父控件的上边缘

第二类属性 属性值必须为id的引用名“@id/id-name”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
android:layout_below:在某控件下方
android:layout_above:在某控件上方
android:layout_toLeftOf:在某控件的左边
android:layout_toRightOf:在某控件的右边
android:layout_alignTop:本控件的上边缘和某控件的上边缘对齐
android:layout_alignLeft:本控件的左边缘和某控件的左边缘对齐
android:layout_alignBottom:本控件的下边缘和某控件的下控件对齐
android:layout_alignRight:本控件的右边缘和某控件的有边缘对齐
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章资源,如无特殊说明或标注,均为本站网友和创作者贡献分享。如若本站内容侵犯了原著者的合法权益,可联系网站客服QQ2743319061删除。

云炬星球 安卓教程 Android开发学习教程(14)- Android布局之相对布局RelativeLayout https://src.yunjunet.cn/876760.html

常见问题
  • 放心亲,我们不会为了几十块钱的东西坏了名声!
查看详情
  • 方法一:点击“立即下载.”按钮,付款后在下载弹窗的虚线框的隐藏信息里获取 方法二:在正文底部使用VIP查看隐藏的解压密码 方法三:联系【云炬网络】公众号客服获取
查看详情
  • 付款后会出现“立即下载”按钮(点击即可下载),如果下载失败也可以联系客服发订单截图补发。
查看详情
  • 登录购买会多端同步购买记录,永久可以查看反复下载;非登录购买仅将购买记录保存到本地浏览器中,浏览器cookie清除后无法再次下载。先右上角点登录,然后点击微信图标可以快速授权注册登录^_^
查看详情
  • 可以试看。点击”查看演示“或“试看预览”按钮可以试读从资料目录中节选的部分内容,也可以自己指定想试看的内容。
查看详情
  • 原因一:本站所有资源已开启有效性检测(服务器24h全自动监测),当监测到下载链接无法访问时会提示“该资源已失效,请勿购买”,遇到这种情况可以联系客服修复失效的下载链接,或直接联系客服在淘宝下单购买即可。(检测原理:购买前服务器程序会预访问下载链接,响应值为200说明资源有效允许购买,响应值为404或502等报错说明资源失效禁止购买)。原因二:上传者未启用“下载”选项。
查看详情
官方客服团队

为您解决烦忧 - 24小时在线 专业服务