安卓布局基础笔记(相对布局 -- RelativeLayout )

Relative Layout


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 of center).

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.

Positioning Views


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.

Some of the many layout properties available to views in a RelativeLayout include:

android:layout_alignParentTop
If  "true", makes the top edge of this view match the top edge of the parent.
android:layout_centerVertical
If  "true", centers this child vertically within its parent.
android:layout_below
Positions the top edge of this view below the view specified with a resource ID.
android:layout_toRightOf
Positions the left edge of this view to the right of the view specified with a resource ID.

These are just a few examples. All layout attributes are documented at RelativeLayout.LayoutParams.

The value for each layout property is either a boolean to enable a layout position relative to the parentRelativeLayout or an ID that references another view in the layout against which the view should be positioned.

In your XML layout, dependencies against other views in the layout can be declared in any order. For example, you can declare that "view1" be positioned below "view2" even if "view2" is the last view declared in the hierarchy. The example below demonstrates such a scenario.

Example


Each of the attributes that control the relative position of each view are emphasized.


原翻译作者:小手冰凉

原文链接:http://developer.android.com/guide/topics/ui/layout/relative.html

RelativeLayout顾名思义,相对布局,在这个容器内部的子元素们可以使用彼此之间的相对位置(例如,在某个视图左边left-of)或者和容器间的相对位置(例如,与父视图左对齐,底部对齐或者居中等)来进行定位。

http://developer.android.com/images/ui/relativelayout.png

RelativeLayout(相对布局)是一个为用户界面设计,非常强大的工具.因为它可以消除嵌套视图组,并保持你的布局层次更简洁,从而提高性能。如果你发现自己使用多个嵌套的LinearLayout组,您可能能够取代单一RelativeLayout。

定位视图

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

相对布局视图的一些可用属性包括:

  • android:layout_alignParentTop
    :如果设置为“true”,使这一视图的顶部边缘匹配父类的顶部边缘

  • android:layout_centerVertical
    :如果“true”,设置此子视图在父视图中垂直居中。

  • android:layout_below
    :设置此视图的上边缘位于通过资源ID指定的视图的下方。

  • android:layout_toRightOf
    :设置此视图的左边缘位于通过资源ID指定的视图的右方。

这仅仅是几个例子,所有的布局属性我们可以在RelativeLayout.LayoutParams中找到。

每个布局属性的值既可以是boolean类型的值来确定布局相对于父布局的位置,也可以是某个子视图的ID,来指定布局相对于这个子视图的位置。

在你的xml布局文件中,依赖于其他视图的布局可以在声明的时候没有顺序。例如:

你可以声明“View1”在“VIew2”的下方,即使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>




http://wiki.eoe.cn/page/Relative_Layout


相对布局属性整理
android:layout_marginTop="25dip" //顶部距离
android:gravity="left" //空间布局位置
android:layout_marginLeft="15dip //距离左边距

// 相对于给定ID控件
android:layout_above 将该控件的底部置于给定ID的控件之上;
android:layout_below 将该控件的底部置于给定ID的控件之下;
android:layout_toLeftOf    将该控件的右边缘与给定ID的控件左边缘对齐;
android:layout_toRightOf  将该控件的左边缘与给定ID的控件右边缘对齐;

android:layout_alignBaseline  将该控件的baseline与给定ID的baseline对齐;
android:layout_alignTop        将该控件的顶部边缘与给定ID的顶部边缘对齐;
android:layout_alignBottom   将该控件的底部边缘与给定ID的底部边缘对齐;
android:layout_alignLeft        将该控件的左边缘与给定ID的左边缘对齐;
android:layout_alignRight      将该控件的右边缘与给定ID的右边缘对齐;

// 相对于父组件
android:layout_alignParentTop      如果为true,将该控件的顶部与其父控件的顶部对齐;
android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐;
android:layout_alignParentLeft      如果为true,将该控件的左部与其父控件的左部对齐;
android:layout_alignParentRight    如果为true,将该控件的右部与其父控件的右部对齐;

// 居中
android:layout_centerHorizontal 如果为true,将该控件的置于水平居中;
android:layout_centerVertical     如果为true,将该控件的置于垂直居中;
android:layout_centerInParent   如果为true,将该控件的置于父控件的中央;

// 指定移动像素
android:layout_marginTop      上偏移的值;
android:layout_marginBottom 下偏移的值;
android:layout_marginLeft   左偏移的值;
android:layout_marginRight   右偏移的值;


还可以分成三类

第一类:属性值为true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物

第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边

android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值