前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >轻松掌握RelativeLayout相对布局

轻松掌握RelativeLayout相对布局

作者头像
分享达人秀
发布2018-02-02 17:50:20
9310
发布2018-02-02 17:50:20
举报
文章被收录于专栏:分享达人秀分享达人秀

在前面三期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局。但在实际开发中使用LinearLayout远远不够,我们本期一起来学习RelativeLayout。

一、认识RelativeLayout

RelativeLayout,又叫相对布局,使用<RelativeLayout>标签。相对布局通常有两种形式,一种是相对于容器而言的,一种是相对于控件而言的。

下表显示了RelativeLayout支持的常用XML属性及相关方法的说明。

XML属性

相关方法

说明

android:gravity

setGravity(int)

设置该布局容器内各子组件的对其方式

android:ignoreGravity

setIgnoreGravity(int)

设置哪个组件不受gravity属性的影响

为了控制该布局容器中各子组件的布局分布,RelativeLayout提供了一个内部类: RelativeLayout.LayoutParams,该类提供了大量的XML属性来控制RelativeLayout布局容器中子组件的布局分布。

在相对于容器定位的属性主要有以下几个,属性值为true或false。

  • android:layout_centerHorizontal:控制该组件是否和布局容器的水平居中。
  • android:layout_centerVertical:控制该组件是否和布局容器的垂直居中。
  • android:layout_centerInparent:控制该组件是否和布局容器的中央位置。
  • android:layout_alignParentTop:控制该组件是否和布局容器的顶部对齐。
  • android:layout_alignParentBottom:控制该组件是否和布局容器的底端对齐。
  • android:layout_alignParentLeft:控制该组件是否和布局容器的左边对齐。
  • android:layout_alignParentRight:控制该组件是否和布局容器的右边对齐。
  • android:layout_alignParentStart:控制该组件是否和布局容器的开始对齐。
  • android:layout_alignParentEnd:控制该组件是否和布局容器的末端对齐。
  • android:layout_alignWithParentIfMissing:如果对应的兄弟组件找不到的话就以父容器做参照物。

在相对于其他组件定位的属性主要有以下几个,属性值为其他组件的id。

  • android:layout_toLeftOf:本组件在某组件的左边。
  • android:layout_toRightOf:本组件在某组件的右边。
  • android:layout_toStartOf:本组件在某组件开始端。
  • android:layout_toEndOf:本组件在某组件末端。
  • android:layout_above:本组件在某组件的上方。
  • android:layout_below:本组件在某组件的下方。
  • android:layout_alignBaseline:本组件和某组件的基线对齐。
  • android:layout_alignTop:本组件的顶部和某组件的的顶部对齐。
  • android:layout_alignBottom:本组件的下边缘和某组件的的下边缘对齐。
  • android:layout_alignRight:本组件的右边缘和某组件的的右边缘对齐。
  • android:layout_alignLeft:本组件左边缘和某组件左边缘对齐。
  • android:layout_alignStart:本组件的开始端和某组件开始端对齐。
  • android:layout_alignEnd:本组件的末端和某组件末端对齐。

除此之外,RelativeLayout.LayoutParams 还继承了 android view. ViewGroup.MarginLayoutParams,因此 RelativeLayout 布局容器中每个子组件也可指定 android.view.ViewGroiip.MarginLayoutParams所支持的各XML属性。

二、示例

接下来通过一个简单的示例程序来学习RelativeLayout的使用用法。

继续使用app/main/res/layout/目录下的activity_main.xml文件,在其中填充如下代码片段:

代码语言:javascript
复制
<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="容器左上侧"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"/>
    <!-- 定义该组件位于父容器上侧水平居中 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="容器上侧水平居中"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>
    <!-- 定义该组件位于父容器右上侧 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="容器右上侧"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"/>
    <!-- 定义该组件位于父容器左侧垂直居中 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左中"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"/>
    <!-- 定义该组件位于父容器右侧垂直居中 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右中"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>
    <!-- 定义该组件位于父容器左下侧 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="容器左下侧"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"/>
    <!-- 定义该组件位于父容器下侧水平居中 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="容器下侧水平居中"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>
    <!-- 定义该组件位于父容器右下侧 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="容器右下侧"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"/>


    <!-- 定义该组件位于父容器中间 -->
    <Button
        android:id="@+id/center_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="容器中央"
        android:layout_centerInParent="true"/>
    <!-- 定义该组件位于center_btn组件的上方 -->
    <Button
        android:id="@+id/center_top_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中上"
        android:layout_above="@id/center_btn"
        android:layout_alignLeft="@id/center_btn"/>
    <!-- 定义该组件位于center_btn组件的下方 -->
    <Button
        android:id="@+id/center_bottom_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中下"
        android:layout_below="@id/center_btn"
        android:layout_alignLeft="@id/center_btn"/>
    <!-- 定义该组件位于center_btn组件的左边 -->
    <Button
        android:id="@+id/center_bottom_left_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中下左"
        android:layout_toLeftOf="@id/center_btn"
        android:layout_alignTop="@id/center_bottom_btn"/>
    <!-- 定义该组件位于center_btn组件的右边 -->
    <Button
        android:id="@+id/center_top_right_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中上右"
        android:layout_toRightOf="@id/center_btn"
        android:layout_alignTop="@id/center_top_btn"/>
</RelativeLayout>

运行程序,可以看到下图所示界面效果:

到此,RelativeLayout的示例结束,关于RelativeLayout的更多用法可以参照上面的XML属性和方法参照表,建议多动手练习。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-08-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 分享达人秀 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档