【Android 开发教程】RelativeLayout相对布局

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


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

[java] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/RLayout"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/lblComments"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:text="Comments" />  
  14.   
  15.     <EditText  
  16.         android:id="@+id/txtComments"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="170px"  
  19.         android:layout_alignLeft="@+id/lblComments"  
  20.         android:layout_below="@+id/lblComments"  
  21.         android:layout_centerHorizontal="true"  
  22.         android:textSize="18sp" />  
  23.   
  24.     <Button  
  25.         android:id="@+id/btnSave"  
  26.         android:layout_width="125px"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_alignRight="@+id/txtComments"  
  29.         android:layout_below="@+id/txtComments"  
  30.         android:text="Save" />  
  31.   
  32.     <Button  
  33.         android:id="@+id/btnCancel"  
  34.         android:layout_width="124px"  
  35.         android:layout_height="wrap_content"  
  36.         android:layout_alignLeft="@+id/txtComments"  
  37.         android:layout_below="@+id/txtComments"  
  38.         android:text="Cancel" />  
  39.   
  40. </RelativeLayout>  
可以观察到,这些views都被嵌在了RelativeLayout里面,每个view都有一些特有的属性去和其他view对准位置。这些属性是:
  • layout_alignParentTop
  • layout_alignParentLeft
  • layout_alignRight
  • layout_below
  • layout_centerHorizontal

这些属性的值,就是每个被引用的view的id。

效果图:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值