首页 技术 正文
技术 2022年11月19日
0 收藏 848 点赞 4,691 浏览 2638 个字

相对布局:RelativeLayout

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

【转】Pro Android学习笔记(二六):用户界面和控制(14):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>

【转】Pro Android学习笔记(二六):用户界面和控制(14):RelativeLayout相互位置校准

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

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

使用ADT的视图工具

【转】Pro Android学习笔记(二六):用户界面和控制(14):RelativeLayout在上图中,我们发现由于第一个控件“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开发相关文章

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291