Pro Android学习笔记(二六):用户界面和控制(14):RelativeLayout

相对布局:RelativeLayout

RelativeLayout也是非常常用的布局,能够精确对控件的位置进行网格对齐,可以设置在控件与其他控件的相对位置,以及控件在容器中的位置。缺省控件的位置为最上面还最左边。下面结合一个例子来进行解说。

<?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="wrap_content" > <!-- 测试:设置wrap_content也会占据满屏或者fill parent -->
 <!--摆放第一个控件:将UserName的TextView放置在最上方,实际上这也是缺省的位置,仅测试属性语法 -->
    <TextView   android:id="@+id/userNameLb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" 
        android:text="UserName:" />
<!-- 第二个控件:将EditText放置在第一个控件的右边,上下的位置,缺省至于最上 -->
    <EditText android:id="@+id/userNameText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_toRightOf="@id/userNameLb1" />
<!-- 第三个控件:由于EditText所占的高度比TextView要大,所以第三个控件,我们放在第二个控件之下,缺省地放置在最左边 -->   
    <TextView android:id="@+id/pwdLb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_below="@id/userNameText" 
        android:text="Password:" />
<!-- 第四个控件:放置在第二个控件之下,以及第三个控件右边。如果我们只设置了放置在第三个控件右边,会发现,该控件和第二个控件出现重叠,因为缺省将控件放置在最上方 -->
     <EditText android:id="@+id/pwdText"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"

         android:layout_alignLeft="@+id/userNameText"
         android:layout_below="@id/userNameText"
/>
<!-- 测试:控件在其他控件的相对位置 -->
    <TextView android:id="@+id/pwdCriteria"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/pwdText"
        android:text="Password Criteria ...." />
<!-- 测试:控件在容器中的相对位置 -->
    <TextView android:id="@+id/disclaimerLb1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"
        android:text="Use at your own risk...... 风险自负"/>    
</RelativeLayout>

相互位置校准

在例子中,我们发现由于TextView和EditView的高度不同,显得不够美观,我们希望将TextView的位置稍微下调,底部基准位和EditView相同。

这里比较特别之处是我们希望第三个控件以第四个控件为基准进行对齐,这是第四个控件我们尚未进行描述,因此我们需要首先在R.java中加入第四个控件的ID,用了@+id/的方式。由于第四个控件的ID具体值已经在之前描述,所以可以使用@id/,但是这里我们仍可以是用@+id/,因+的含义是,如果不存在则增加,如存在则使用。

使用ADT的视图工具

在上图中,我们发现由于第一个控件“UserName:”和第二个控件“Password:”的文字长度不同,导致两个EditText并不对齐。在布局中,我们可以通过ADT的视图工具来查看对齐情况,我们也可以用ADT视图工具直接进行调整。

通过鼠标对控件的拉伸进行调整,检查xml文件,对于第四个控件,加上了android:laytou_alignLeft的属性。

<EditText android:id="@id/pwdText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:layout_alignLeft="@+id/userNameText"
    android:layout_below="@id/userNameText" />


相关链接: 我的Android开发相关文章

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值