[Android]相对布局

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).
相对布局是显示子视图之间相对位置的视图组。每个视图的位置可以被指定为相对于同级元素(如左边或下面的另一个观点)或位置相对于父视图的位置(如对齐左边或底部中心)。

A RelativeLayout is a very powerful utility for designing a user interface because it can eliminate nested view groups and keep your layout hierarchy flat, which improves performance. If you find yourself using several nested LinearLayout groups, you may be able to replace them with a single RelativeLayout.
相对布局是用户界面设计的一个非常强大的工具,因为它可以消除嵌套视图组和保持你的布局层次扁平,从而提高性能。如果你发现自己使用的几个嵌套的元素组,你可以用一个相对布局。

RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.LayoutParams.
相对布局允许子元素指定其相对于父视图或彼此(指定的ID)的位置。所以你可以通过右边的边界对齐2个元素,或者让一个子视图在另一个子视图下面,以屏幕为中心,以左为中心。默认情况下,所有的子视图的绘制在版面的左上方,所以你必须定义每个视图的位置,通过使用relativelayout.layoutparams中的各种布局属性。

RelativeLayout.LayoutParams中包含的布局属性包含以下:
android:layout_above 本组件在某组件的上方
android:layout_alignBaseline 本组件和某组件的基线对齐。
android:layout_alignBottom 本组件的下边缘和某组件的的下边缘对齐
android:layout_alignEnd 本组件的末端和某组件末端对齐
android:layout_alignRight 本组件的右边缘和某组件的的右边缘对齐
android:layout_alignLeft 本组件左边缘和某组件左边缘对齐
android:layout_alignStart 本组件的开始端和某组件开始端对齐
android:layout_alignTop 本组件的顶部和某组件的的顶部对齐
android:layout_below 本组件在某组件的下方

android:layout_toEndOf 本组件在某组件末端
android:layout_toLeftOf 本组件在某组件的左边
android:layout_toRightOf 本组件在某组件的右边
android:layout_alignLeft 本组件在某组件开始端

下面是相对布局的demo:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >
    <EditText
        android:id="@+id/name"
        android:layout_width="match_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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值