Android LinearLayout和RelativeLayout

LinearLayout:

LinearLayout是一个ViewGroup, 它的特点是将所有的子View(或者子ViewGroup)线性依次排列, 可以通过android:orientation属性指定排列方向是垂直或者水平.


所以水平排列的LinearLayout只有一行, 而垂直排列的则只有一列.

 

LayoutWeight:

LinearLayout可以支持一个android:layout_weight属性, 该属性指定某View拥有一个描述”重要性”的参数, 以描述它应该占有多大的剩余屏幕尺寸. 一个较大的weight可以让该View占据更大的剩余空间, 子View通过指定weight可以占据所有的剩余空间, 默认值是0.

比如如果有三个文本框, 其中两个设置weight为1, 第三个不设置, 也就是默认值0, 那么第三个文本框将只占据自己内容大小的空间, 两个设置了weight的文本框将扩大自己的尺寸并平分剩下的空间. 如果第三个将自己的weight改为2, 那么它将占据所有空间的一半, 剩下的两个weight为1的则各占据其余空间的一半.

 

栗子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical" >
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/to" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/subject" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="@string/message" />
    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/send" />
</LinearLayout>

这里只有第三个EditText设置了layout_weight, 所以它将独占屏幕的”空余空间”, 上面代码将在屏幕上显示成这样:


 

RelativeLayout:

RelativeLayout是一个ViewGroup,在它内部的子View(或者子ViewGroup)根据相对位置排列, 排列的位置可以依据跟它们同级的View(比如在一个View的左边或者下边), 也可以是父级RelativeLayout的位置决定(比如底部对齐, 左侧对齐或者中间对齐等).


RelativeLayout很强大, 因为它不需要嵌套很多层就可以实现复杂的布局, 如果我们发现自己使用了多层嵌套的LinearLayout, 那么应该考虑使用一个RelativeLayout来代替它们. 应该避免使用多层嵌套的ViewGroup.

 

View定位:

RelativeLayout内部的View可以指定自己相对于RelativeLayout本身或者其它View的相对位置(通过ID). 默认情况下所有的子View都会被放在左上角, 所以必须通过各子View的RelativeLayout.LayoutParams参数来设置它们的位置. 下面是一些可用的属性:

android:layout_alignParentTop: 如果为true, 表示跟父容器顶部对齐.

android:layout_centerVertical: 如果为true, 表示相对父容器垂直中间对齐.

android:layout_below: 该值指定一个资源ID, 使拥有本属性的View的顶部在这个指定ID的View下.

android:layout_toRightOf: 此View的左侧与指定的资源ID的View的右侧对齐.

这样的属性还有挺多, 可以参考这里. 它们的值要么是boolean类型的指定与父容器的关系, 要么就是资源ID型, 指定与同级View的关系. 我们可以排列View1在View2的下面等关系

栗子:

<?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"
    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>

它显示成这样:



参考: http://developer.android.com/guide/topics/ui/layout/linear.html

          http://developer.android.com/guide/topics/ui/layout/relative.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值