跳转至

RelativeLayout 相对布局

简述RelativeLayout的使用方式和特点。给出一些示例看看RelativeLayout怎么用。

RelativeLayout 简述

RelativeLayout和LinearLayout类似,都是ViewGroup,能“容纳”多个子view。 RelativeLayout 是一个以相对位置显示子视图的视图组。每个视图的位置可以指定为相对于同级元素的位置(例如,在另一个视图的左侧或下方)或相对于父级 RelativeLayout 区域的位置(例如在底部、左侧或中心对齐)。

子view可以是TextView,Button,或者是LinearLayout,RelativeLayout等等。 如果不添加其他配置,它们默认是在RelativeLayout的左上角。 在RelativeLayout中,子View可以根据另一个子View来确定位置。 但必须注意的是,RelativeLayout和它的子View不能互相依赖。比如RelativeLayout设置高度为wrap_content,子View设置了ALIGN_PARENT_BOTTOM,这样你会发现RelativeLayout被撑到最大。 RelativeLayout能消除嵌套视图组并使布局层次结构保持扁平化。

接下来介绍一些在xml中的设置。

属性介绍

RelativeLayout 可以指定子视图相对于父视图或彼此(由 ID 确定)的位置。因此,您可以按照右边框对齐两个元素,或者使它们一上一下,屏幕居中,左侧居中,等等。默认情况下,所有子视图均绘制在布局的左上角,因此您必须使用 RelativeLayout.LayoutParams 中提供的各种布局属性定义每个视图的位置。

有很多布局属性可用于 RelativeLayout 中的视图,部分示例包括:

  • android:layout_alignParentTop
    • 如果为 "true",会将此视图的上边缘与父视图的上边缘对齐。
  • android:layout_centerVertical
    • 如果为 "true",会将此子级在父级内垂直居中。
  • android:layout_below
    • 将此视图的上边缘放置在使用资源 ID 指定的视图下方。
  • android:layout_toRightOf
    • 将此视图的左边缘放置在使用资源 ID 指定的视图右侧。

这些只是几个示例。所有布局属性都记录在 RelativeLayout.LayoutParams

示例

为了让UI好看一点,先定义一下样式,在style.xml文件中新增一个style。

    <style name="RelativeLayoutDemo1Item">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:padding">4dp</item>
        <item name="android:background">@color/colorAccent</item>
        <item name="android:textColor">#ffffff</item>
        <item name="android:textSize">12sp</item>
    </style>

示例1

在layout中增加RelativeLayout与一些子View。 子View设置了不同的属性,分布在父View的上下左右中各个地方。

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <TextView
            style="@style/RelativeLayoutDemo1Item"
            android:text="default" />

        <TextView
            style="@style/RelativeLayoutDemo1Item"
            android:layout_alignParentEnd="true"
            android:text="layout_alignParentEnd" />

        <TextView
            style="@style/RelativeLayoutDemo1Item"
            android:layout_centerInParent="true"
            android:text="layout_centerInParent" />

        <TextView
            style="@style/RelativeLayoutDemo1Item"
            android:layout_alignParentBottom="true"
            android:text="layout_alignParentBottom" />

        <TextView
            style="@style/RelativeLayoutDemo1Item"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:text="layout_alignParentBottom | End" />

    </RelativeLayout>

RelativeLayout demo1

示例2

子View可以把另外的子View当做位置依据。

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="120dp">

        <TextView
            android:id="@+id/tv1"
            style="@style/RelativeLayoutDemo1Item"
            android:text="R" />

        <TextView
            android:id="@+id/tv2"
            style="@style/RelativeLayoutDemo1Item"
            android:layout_below="@id/tv1"
            android:layout_toEndOf="@id/tv1"
            android:text="u" />

        <TextView
            android:id="@+id/tv3"
            style="@style/RelativeLayoutDemo1Item"
            android:layout_below="@id/tv2"
            android:layout_toEndOf="@id/tv2"
            android:text="s" />

        <TextView
            android:id="@+id/tv4"
            style="@style/RelativeLayoutDemo1Item"
            android:layout_below="@id/tv3"
            android:layout_toEndOf="@id/tv3"
            android:text="t" />

    </RelativeLayout>
RelativeLayout demo1

更多请参考

RelativeLayout View.MeasureSpec MeasureSpec简述

本站说明

一起在知识的海洋里呛水吧。广告内容与本站无关。如果喜欢本站内容,欢迎投喂作者,谢谢支持服务器。如有疑问和建议,欢迎在下方评论~

📖AndroidTutorial 📚AndroidTutorial 🙋反馈问题 🔥最近更新 🍪投喂作者

Ads