灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2582回复:0

Android 程式开发:(五)屏幕组件 —— 5.5 RelativeLayout相对布局

楼主#
更多 发布于:2012-09-06 14:04

使用RelativeLayout,可以通过设置“相对位置”(每个View相对于另一个view的位置),来指定它所包含的子view的位置。看下面main.xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:id="@+id/RLayout"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent" >

    <TextView
        Android:id="@+id/lblComments"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_alignParentLeft="true"
        Android:layout_alignParentTop="true"
        Android:text="Comments" />

    <EditText  www.atcpu.com
        Android:id="@+id/txtComments"
        Android:layout_width="fill_parent"
        Android:layout_height="170px"
        Android:layout_alignLeft="@+id/lblComments"
        Android:layout_below="@+id/lblComments"
        Android:layout_centerHorizontal="true"
        Android:textSize="18sp" />

    <Button
        Android:id="@+id/btnSave"
        Android:layout_width="125px"
        Android:layout_height="wrap_content"
        Android:layout_alignRight="@+id/txtComments"
        Android:layout_below="@+id/txtComments"
        Android:text="Save" />

    <Button
        Android:id="@+id/btnCancel"
        Android:layout_width="124px"
        Android:layout_height="wrap_content"
        Android:layout_alignLeft="@+id/txtComments"
        Android:layout_below="@+id/txtComments"
        Android:text="Cancel" />

</RelativeLayout>
可以观察到,这些views都被嵌在了RelativeLayout里面,每个view都有一些特有的属性去和其他view对准位置。这些属性是:
layout_alignParentTop
layout_alignParentLeft
layout_alignRight
layout_below
layout_centerHorizontal
这些属性的值,就是每个被的view的id。

效果图:






摘自 manoel的专栏

喜欢0 评分0
游客

返回顶部