相对布局: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开发相关文章

转载于:https://www.cnblogs.com/blongfree/p/5047942.html

【转】Pro Android学习笔记(二六):用户界面和控制(14):RelativeLayout相关推荐

  1. pro android学习笔记,Pro Android学习笔记(六八):HTTP服务(2):HTTP POST-Go语言中文社区...

    BufferedReader in = null; try{ //[Step 1]创建一个HttpClient的对象(或使用已有的) HttpClient client = new DefaultHt ...

  2. 【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout...

    目录(?)[-] 布局Layout 线性布局LinearLayout 表格布局TableLayout 布局Layout Layout是容器,用于对所包含的view进行布局.layout是view的子类 ...

  3. Pro Android学习笔记(二九):用户界面和控制(17):include和merge

    xml控件代码重用:include 如果我们定义一个控件,需要在不同的layout中重复使用,或者在同一个layout中重复使用,可以采用include的方式.例如定义my_button.xml如下 ...

  4. 【转】 Pro Android学习笔记(二九):用户界面和控制(17):include和merge

    目录(?)[-] xml控件代码重用include xml控件代码重用merge 横屏和竖屏landsacpe portrait xml控件代码重用:include 如果我们定义一个控件,需要在不同的 ...

  5. Pro Android学习笔记(三三):Menu(4):Alternative菜单

    什么是Alternative menu(替代菜单) 举个例子,Activity显示一个文本文件.如果用户想对文本文件进行编辑,Activity不提供编辑能力,但可由其他activity或者其他应用提供 ...

  6. 【转】 Pro Android学习笔记(二十):用户界面和控制(8):GridView和Spinner

    目录(?)[-] GridView Spinner GridView GridView是网格状布局,如图所示.在了解ListView后,很容易了解GridView.下面是例子的XML文件. <? ...

  7. Pro Android学习笔记(七七):服务(2):Local Service

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ Local Service的目的是更容易实 ...

  8. Pro Android学习笔记(一五五) 传感器(5) 磁场传感器和方位(上)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 文章转载 ...

  9. Pro Android学习笔记(一五五):传感器(5): 磁场传感器和方位(上)

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处http://blog.csdn.net/flowingflying/以及作者@恺风Wei. 磁场传感器(Magne ...

  10. Pro Android学习笔记 四八 ActionBar 1 Home图标区

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Acti ...

最新文章

  1. 所有库在门不显示封装_奈雪和石库门在一起,太上头
  2. 来自法国的山寨苹果系统——梨子系统PearOS,精美仿苹果风格的免费Linux操作系统(颇有iOS和OSX的神...
  3. 【控制】滑动模型控制(Sliding Mode Control)
  4. html指定ie内核,指定Webbrowser控件所用IE内核版本
  5. 2015 10月21日 工作计划与执行
  6. 大话设计模式—单例模式
  7. one order event trace - how to switch on
  8. 爬虫cookie过期_python instagram 爬虫
  9. 一张图理解JS的原型(prototype、_proto_、constructor的三角关系)
  10. file input 移动端选择文件夹_免费 |《MNN For Swift》移动端机器学习实战课程
  11. layui按条件隐藏表格列
  12. bzoj 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛
  13. java 定时器 的中断程序,AVR单片机教程——定时器中断
  14. lammps计算聚合物例子_lammps计算金属扩散
  15. oracle数据透明加密,使用ORACLE 透明数据加密 TDE
  16. 将.bat文件设置为Window系统开机自启动项
  17. 网络电视服务器账号密码,中兴网络电视机顶盒密码是多少
  18. 条码打印机无法正常打印该如何解决
  19. 计算机操作员要求,计算机操作员教学大纲
  20. 【软件工程】交付和维护

热门文章

  1. [leetcode]01.04. 回文排列
  2. OSI七层协议模型和各自的功能
  3. 吴恩达神经网络和深度学习-学习笔记-39-计算机视觉现状
  4. python机器学习库sklearn——神经网络
  5. java中mydoc_实验二/MyDoc.java · 20175326李一潇/20175326java - Gitee.com
  6. win10 安装msys2 和 ruby
  7. opencv生成灰度图并保存
  8. FastReport 数据区二级显示
  9. ios之JavaScript
  10. Java获取当前时间(二)