Relative Layout(相对布局)

相对布局

RelativeLayout 是一种用相对位置显示所有子元素视图的ViewGroup。每个视图的位置都可以通过相对于相邻元素的位置来指定(例如在另一个视图的左边或下面),或相对于在父元素 RelativeLayout 区域中的位置指定(例如底部,左边或居中对齐)。

RelativeLayout 对于设计用户界面来说是很强大实用的,因为它不需要嵌套ViewGroup并且使你的布局扁平化,这样可以提升性能。如果你发现自己使用了很多嵌套的 LinearLayout 组,那么你可以使用一个 RelativeLayout 替代它们。

定位视图


RelativeLayout 可以让子元素视图相对于父元素视图或其他子元素视图的位置来指定它们的位置(通过ID指定)。所以你可以通过右边界对齐两个元素,或使一个在另一个下方,在屏幕居中,据中偏左等等。默认情况下,所有的子元素视图都在布局的左上角,所以你必须使用 RelativeLayout.LayoutParams 里可用的各种布局属性定义每个视图的位置。

在 RelativeLayout 里包含许多视图可用的布局属性中的一些:

android:layout_alignParentTop
"true",使视图的顶边与父元素的顶边对齐。
android:layout_centerVertical
"true",使这个子元素在父元素中垂直居中。
android:layout_below
使该视图的顶边处于资源ID指定的视图之下。
android:layout_toRightOf
使视图的左侧边缘处于资源ID指定的视图右侧。

这只是一些事例。所有的布局属性在 RelativeLayout.LayoutParams 里都有说明。

每个布局属性的值不是可以开启相对于父元素 RelativeLayout 的布局位置的布尔值,就是该视图用来参考放置的另外的视图的ID。

在你的XML布局中,被依赖的视图可以在布局中的任何位置声明。例如,尽管“view2”在层级中是最后一个被声明的视图,你仍然可以声明“view1”在“view2”的下方。下面的实例演示了这样的场景。

事例


控制每个视图相对位置的属性被加粗显示了。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >
    <EditText
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/reminder" />
    <Spinner
        android:id="@+id/dates"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/times" />
    <Spinner
        android:id="@id/times"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_alignParentRight="true" />
    <Button
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/times"
        android:layout_alignParentRight="true"
        android:text="@string/done" />
</RelativeLayout>

请查阅 RelativeLayout.LayoutParams 了解更多关于 RelativeLayout 中子元素视图所有可用布局属性的细节。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用相对布局实现最左边一个图片,图片右边围绕着两个TextView,最右边一个按钮的代码: ``` <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 最左边的图片 --> <ImageView android:id="@+id/image_left" android:layout_width="wrap_content" android:layout_height="match_parent" android:src="@drawable/image_left" android:scaleType="fitCenter"/> <!-- 右边的两个TextView --> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Textview 1" android:layout_toRightOf="@id/image_left" android:layout_alignTop="@id/image_left" android:layout_marginLeft="16dp"/> <TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Textview 2" android:layout_toRightOf="@id/image_left" android:layout_below="@id/textview1" android:layout_marginLeft="16dp" android:layout_marginTop="8dp"/> <!-- 最右边的按钮 --> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="16dp"/> </RelativeLayout> ``` 在这个布局中,我们首先使用相对布局,将最左边的图片放在最下面,并为它设置一个id。然后,我们将右边的两个TextView分别放在图片的右边,并使用 `android:layout_toRightOf` 和 `android:layout_below` 属性来确定它们的位置关系。最后,我们将最右边的按钮放在布局的右边,并使用 `android:layout_alignParentRight` 和 `android:layout_centerVertical` 属性来确定它在布局中的位置。这样,我们就用相对布局实现了这个布局,保证了各个控件之间的位置关系。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值