源码教程 2025年06月7日
0 收藏 0 点赞 169 浏览 2559 个字
摘要 :

RelativeLayout 又称相对布局。从名称上可以看出,这种布局方式是以一种让组件以相对于容器或者相对于容器中的另一个组件的相对位置进行放置的布局方式。RelativeLayout ……

RelativeLayout 又称相对布局。从名称上可以看出,这种布局方式是以一种让组件以相对于容器或者相对于容器中的另一个组件的相对位置进行放置的布局方式。

RelativeLayout 布局提供了一些常用的布局设置属性用于确定组件在视图中的相对位置。下面列举了 RelativeLayout 相关属性及其所代表的含义。


RelativeLayout 布局常用属性

属性 描述
android:layout_above="@id/xxx" 将控件置于给定 ID 控件之上
android:layout_below="@id/xxx" 将控件置于给定 ID 控件之下
android:layout_toLeftOf="@id/xxx" 将控件的右边缘和给定 ID 控件的左边缘对齐
android:layout_toRightOf="@id/xxx" 将控件的左边缘和给定 ID 控件的右边缘对齐
android:layout_alignBaseline="@id/xxx" 将控件的 baseline 与给定 ID 的 baseline 对齐
android:layout_alignTop="@id/xxx" 将控件的上边缘和给定 ID 控件的上边缘对齐
android:layout_alignBottom="@id/xxx" 将控件的底边缘和给定 ID 控件的底边缘对齐
android:layout_alignLeft="@id/xxx"  将控件的左边缘和给定 ID 控件的左边缘对齐
android:layout_alignRight="@id/xxx" 将控件的右边缘和给定 ID 控件的右边缘对齐
android:layout_alignParentLeft="true" 将控件的左边缘和父控件的左边缘对齐
android:layout_alignParentTop="true" 将控件的上边缘和父控件的上边缘对齐
android:layout_alignParentRight="true" 将控件的右边缘和父控件的右边缘对齐
android:layout_alignParentBottom="true" 将控件的底边缘和父控件的底边缘对齐
android:layout_centerInParent="true" 将控件置于父控件的中心位置
android:layout_centerHorizontal="true" 将控件置于水平方向的中心位置
android:layout_centerVertical="true" 将控件置于垂直方向的中心位置

实例 RelativeLayoutDemo 演示了相对布局的使用方法,其运行效果如图 5 所示。

Android五大布局之相对布局RelativeLayout使用

图 5  RelativeLayout 布局效果

实例 RelativeLayoutDemo 中的布局文件 main.xml 代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="fill_parent"

  4. android:layout_height="fill_parent">

  5.    <TextView

  6.         android:id="@+id/label"

  7.         android:layout_width="fill_parent"

  8.         android:layout_height="wrap_content"

  9.         android:text="@string/hello" />

  10.    <EditText

  11.         android:id="@+id/enter"

  12.         android:layout_width="fill_parent"

  13.         android:layout_height="wrap_content"

  14.         android:layout_alignParentLeft="true"

  15.         android:layout_below="@+id/label" />

  16.    <Button

  17.         android:id="@+id/button1"

  18.         android:layout_width="wrap_content"

  19.         android:layout_height="wrap_content"

  20.         android:layout_alignParentRight="true"

  21.         android:layout_below="@+id/enter"

  22.         android:text="@string/butltext" />

  23.    <Button

  24.         android:id="@+id/ok"

  25.         android:layout_width="wrap_content"

  26.         android:layout_height="wrap_content"

  27.         android:layout_alignBottom="@+id/button1"

  28.         android:layout_alignParentLeft="true"

  29.         android:text="@string/but2text" />

  30. </RelativeLayout>

该 RelativeLayout 布局的过程如下:

1)放置一个 ID 为 label 的 TextView 组件。

2)通过 android:layout_below="@+id/label" 属性将 ID 为 enter 的组件 EditText 放置到 TextView 的下面。

3)在布局中加入一个 ID 为 button1 的 Button,通过 android:layout_below="@+id/enter" 属性将该 Button 放置到 enter 的下面,通过 android:layout_alignParentRight= "true" 属性将 Button 放置到相对布局的右侧。

4)在相对布局中加入一个名为 ok 的 Button,通过 android:layout_alignBottom="@+ id/button1" 属性将该 Button 底边与 button1 对齐,通过 android:layout_alignParentLeft ="true" 属性将该 Button 放置到布局的左边。

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/948.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

700
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

714
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

306
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,038
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

835
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

723
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号