[转]Android布局之RelativeLayout(相对布局)

相对布局由于属性比较多,所以用起来有些费力,但灵活性较其他布局方法好,所以掌握好相对布局将会非常有用。先看等一个例子:

1 <? xml version = " 1.0 " encoding = " utf-8 " ?>
2 < RelativeLayout xmlns:android = " http://schemas.android.com/apk/res/android "
3 android:layout_width = " fill_parent "
4 android:layout_height = " fill_parent " >
5 < TextView
6 android:id = " @+id/label "
7 android:layout_width = " fill_parent "
8 android:layout_height = " wrap_content "
9 android:text = " Type here: " />
10 < EditText
11 android:id = " @+id/entry "
12 android:layout_width = " fill_parent "
13 android:layout_height = " wrap_content "
14 android:background = " @android:drawable/editbox_background "
15 android:layout_below = " @id/label " />
16 < Button
17 android:id = " @+id/ok "
18 android:layout_width = " wrap_content "
19 android:layout_height = " wrap_content "
20 android:layout_below = " @id/entry "
21 android:layout_alignParentRight = " true "
22 android:layout_marginLeft = " 10dip "
23 android:text = " OK " />
24 < Button
25 android:layout_width = " wrap_content "
26 android:layout_height = " wrap_content "
27 android:layout_toLeftOf = " @id/ok "
28 android:layout_alignTop = " @id/ok "
29 android:text = " Cancel " />
30 </ RelativeLayout >
这是很常见的布局内容,讲解如下:
1  android:layout_below = " @id/label " />

将当前控件放置于id为label 的控件下方。

1 android:layout_alignParentRight = " true "

使当前控件的右端和父控件的右端对齐。这里属性值只能为true或false,默认false。

1 android:layout_marginLeft = " 10dip "

使当前控件左边空出相应的空间。

1 android:layout_toLeftOf = " @id/ok "

使当前控件置于id为ok的控件的左边。

1 android:layout_alignTop = " @id/ok "

使当前控件与id控件的上端对齐。

至此,我们已经发现,其属性之繁多。下面简单归纳一下:
第一类:属性值为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
第三类:属性值为具体的像素值,如30dip,40px
*android:layout_marginBottom
*android:layout_marginLeft
*android:layout_marginRight
*android:layout_marginTop

再看第二个例子:

1 <? xml version = " 1.0 " encoding = " utf-8 " ?>
2 < RelativeLayout
3 xmlns:android = " http://schemas.android.com/apk/res/android "
4 android:layout_width = " fill_parent "
5 android:layout_height = " fill_parent " >
6
7 < AnalogClock
8 android:id = " @+id/aclock "
9 android:layout_width = " wrap_content "
10 android:layout_height = " wrap_content "
11 android:layout_centerInParent = " true " />
12 < DigitalClock
13 android:id = " @+id/dclock "
14 android:layout_width = " wrap_content "
15 android:layout_height = " wrap_content "
16 android:layout_below = " @id/aclock "
17 android:layout_alignLeft = " @id/aclock "
18 android:layout_marginLeft = " 40px " />
19 < TextView
20 android:layout_width = " wrap_content "
21 android:layout_height = " wrap_content "
22 android:text = " 当前时间: "
23 android:layout_toLeftOf = " @id/dclock "
24 android:layout_alignTop = " @id/aclock " />
25 </ RelativeLayout >

两个例子的效果图如下:第一个例子

968161cbb07c3b7bf31fe70d.jpg

第二个例子
47b6e7dbe36608e6b7fd483a.jpg
































转载于:https://www.cnblogs.com/keis/archive/2011/03/16/1986356.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值