zoukankan      html  css  js  c++  java
  • 【转】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开发相关文章

  • 相关阅读:
    ITK+VTK+VS环境搭建.Q:vs编译出问题参见VTK(一)哈。
    shell按关键字批量杀进程
    lua使用笔记
    for (; ; )和while (true) 没有区别
    git笔记
    ArrayList LinkedList
    面试整理
    这个相机不错
    idea激活服务器
    git 笔记
  • 原文地址:https://www.cnblogs.com/blongfree/p/5047942.html
Copyright © 2011-2022 走看看