前面我们分别介绍和学习了LinearLayout(线性布局)、FrameLayout(单帧布局)和AbsoluteLayout(绝对布局)。这次我们要进行RelativeLayout(相对布局)和TableLayout(表格布局)的学习。

这部分是非常重要的知识点。RelativeLayout是开发过程中强烈建议使用的,而TableLayout是满足一些特定需求时(常见表格显示,但不局限于此)须要使用。

【博客专栏:http://blog.csdn.net/column/details/alearning.html

RelativeLayout(相对布局)

       前面介绍的LinearLayout(现象布局)与即将介绍的RelativeLayout(相对布局)。是实际开发中较常使用的,也是开发过程中强烈建议的使用。以下我们来開始了解RelativeLayout布局。

       相对布局。顾名思义就是以“相对”位置/对齐为基础的布局方式。

本例拓展的属性配置是:

  • android:id   指定该控件唯一标志的ID值;
  • android:layout_above    指定该控件的底部置于给定ID的控件之上;
  • android:layout_below    指定该控件的底部置于给定ID的控件之下;
  • android:layout_toLeftOf  指定该控件的右边缘与给定ID的控件左边缘对齐;
  • android:layout_toRightOf 指定该控件的左边缘与给定ID的控件右边缘对齐;
  • android:layout_alignBaseline 指定该控件的baseline与给定ID的baseline对齐;
  • android:layout_alignTop     指定该控件的顶部边缘与给定ID的顶部边缘对齐;
  • android:layout_alignBottom  指定该控件的底部边缘与给定ID的底部边缘对齐;
  • android:layout_alignLeft     指定该控件的左边缘与给定ID的左边缘对齐;
  • android:layout_alignRight    指定该控件的右边缘与给定ID的右边缘对齐;
  • android:layout_alignParentTop指定该控件的顶部与其父控件的顶部对齐;
  • android:layout_alignParentBottom 指定该控件的底部与其父控件的底部对齐;
  • android:layout_alignParentLeft    指定该控件的左部与其父控件的左部对齐;
  • android:layout_alignParentRight   指定该控件的右部与其父控件的右部对齐;

注:相对布局的属相设置较多,在实际的开发中须要不断的补充与拓展。

【布局文件】activity_relativelayout.xml
<?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="match_parent" >
<!-- 该控件位于界面中部 -->
<TextView
android:id="@+id/red"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerInParent="true"
android:background="#CD2E57"
android:gravity="center"
android:text="Red" /> <!-- 该控件的左边缘与给定id为red控件的左边缘对齐 -->
<TextView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignLeft="@id/red"
android:background="#64DB99"
android:gravity="center"
android:text="Green" /> <!-- 该控件位于界面底部 同一时候 位于给定id为red控件的下边/右边(即右下位置) -->
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@id/red"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/red"
android:background="#FFFA78"
android:gravity="center"
android:text="Yellow" /> <!-- 该控件位于给定id为red控件的左边 -->
<TextView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@id/red"
android:background="#148CFF"
android:gravity="center"
android:text="Blue" />
</RelativeLayout>

效果截图:


TableLayout(表格布局)

       TableLayout表格布局,表格布局类似Html里面的Table。每个TableLayout里面有表格行TableRow(类似于Html中Table里的tr)。TableRow里面能够详细定义每个元素,比如TextView。设定他的对齐方式android:gravity="center"。

本例拓展的属性配置是:

  • android:stretchColumns    指定可伸展的列。该列能够向行方向伸展,最多可占领一整行。
  • android:shrinkColumns     指定可收缩的列。当该列子控件的内容太多。已经挤满所在行,那么该子控件的内容将往列方向显示。

  • android:collapseColumns  指定要隐藏的列。

演示样例:

android:stretchColumns="0"           第0列可伸展

android:shrinkColumns="1,2"         第1,2列皆可收缩

android:collapseColumns="*"         隐藏全部行

注:列能够同一时候具备stretchColumns及shrinkColumns属性。若此,那么当该列的内容N多时,将“多行”显示其内容。(这里不是真正的多行,而是系统依据须要自己主动调节该行的layout_height)。

  • android:layout_column    指定该单元格在第几列显示
  • android:layout_span      指定该单元格占领的列数(未指定时,为1)

演示样例:

android:layout_column="1"    该控件显示在第1列

android:layout_span="2"        该控件占领2列

说明:一个控件也能够同一时候具备这两个特性。

【布局文件】activity_relativelayout.xml。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0,1,2" > <TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#6495ED"
android:padding="2dp"
android:text="文本1-1" /> <TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#B0C4DE"
android:padding="2dp"
android:text="文本1-2" /> <TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#32CD32"
android:padding="2dp"
android:text="文本1-3" /> <TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#FFD700"
android:padding="2dp"
android:text="文本1-4" />
</TableRow> <TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_height="40dp"
android:layout_column="1"
android:layout_span="2"
android:background="#FF8C00"
android:text="跨列文本2-2,3跨2到3列" />
</TableRow> <TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_height="40dp"
android:layout_column="0"
android:layout_span="2"
android:background="#FF69B4"
android:text="跨列文本3-1,2跨1到2列" />
</TableRow> <!-- -->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:collapseColumns="2"
android:shrinkColumns="1"
android:stretchColumns="0" > <TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:background="#696969"
android:textColor="@android:color/white"
android:text="行方向伸展文本,可自行增长文本查看效果!" /> <TextView
android:textColor="@android:color/white"
android:background="#800000"
android:text="列方向伸展文本,可自行增长文本查看效果!" />
</TableRow>
</TableLayout> </TableLayout>

效果截图:


        到此,基于XML配置的五种经常使用的布局。已经所有解说完毕。可是布局的设计方式不止一种,另一种是基于Java(Android)开发语言的设置。

这部分。将会在接下来的不断的学习中了解与使用。

敬请各位看官的继续关注。


參考资料

4、http://blog.sina.com.cn/s/blog_63c66eb60100u29p.html

5、http://blog.csdn.net/beyond0525/article/details/8841139

【ALearning】第四章 Android Layout组件布局(二)的更多相关文章

  1. 【ALearning】第四章 Android Layout组件布局(一)

    在本章中,我们将Android学习组件布局.在前面的章节,我们也开始使用LinearLayout布局.然后我们在布局文件更加具体的学习和理解,会. Android的界面是有布局和组件协同完毕的,布局好 ...

  2. 第四章Android移植环境搭建

    第四章Android移植环境搭建 这一章主要学习如何搭建 Android 移植的环境.因为 Android 底层是基于 Linux 内核的,所以本章从交叉编译环境等嵌入式开发环境的搭建开始,介绍了 B ...

  3. 第四章 MySQL高级查询(二)

    第四章 MySQL高级查询(二) 一.EXISTS子查询 在执行create 或drop语句之前,可以使用exists语句判断该数据库对像是否存在,返回值是true或false.除此之外,exists ...

  4. 【Android开发日记】之入门篇(四)——Android四大组件之Activity

    在Android中,无论是开发者还是用户,接触最多的就算是Activity.它是Android中最复杂.最核心的组件.Activity组件是负责与用户进行交互的组件,它的设计理念在很多方面都和Web页 ...

  5. 第四章 android 命名规范和编码规范

    书里面讲的比较常见,单个人也是有不同的观点: 因为android绝大部分使用java开发的,因此java相关规范适用于android: Google Style: 英文地址:http://google ...

  6. [Learn AF3]第四章 App framework组件之Button

    Button    组件名称:Button     是否js控件:否     使用说明:如果说panel组件是af3的“核心(heart of the ui)”,那么Button就是af中的五虎上将之 ...

  7. android layout的布局

    1.android:layout_width.android:layout_heigth 表示控件的大小,长宽 2.andoid:layout_gravity .android:gravity表示控件 ...

  8. Android笔记(五十四) Android四大组件之一——ContentProvider(一)

    ContentProvider提供数据 在Android中,他的每个应用都是相互独立的,各自运行在自己的Dalvik虚拟机中,但现实使用中常常需要在多个应用之间进行数据交换,例如发短信需要获取联系人中 ...

  9. Android教材 | 第三章 Android界面事件处理(二)—— 杰瑞教育原创教材试读

     编者按 JRedu 杰瑞教育原创系列教材将于年后与大家正式见面.为更好的借鉴读者意见,我们将会陆续地在博客园推出一系列教材试读.我们也热忱的欢迎广大博友与我们互动,提出宝贵意见. 本篇博客将推出教材 ...

随机推荐

  1. Sparse Autoencoder(二)

    Gradient checking and advanced optimization In this section, we describe a method for numerically ch ...

  2. vue 遇到的报错

    [Vue warn]: Invalid default value for prop "dataParams": Props with type Object/Array must ...

  3. Ubuntu+PyQt5+Python3.6+Qt Designer 实现可视化窗口的编辑

    一.为什么写这片博文 近期将实验室的电脑的OS换成了ubuntu,想对linux进一步的了解和使用.在使用的过程中想用python+pyqt5写一个音乐播放器和视频播放器(这也是linux的乐趣所在) ...

  4. FormatMessage函数的使用方法

    使用FormatMessage时假设对一些參数不细致研究.那么就会出错误.首先说下这个函数 1 函数描写叙述 DWORD WINAPI FormatMessage( _In_ DWORD dwFlag ...

  5. js05---js实现Map

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  6. ubuntu-通配符

    ubuntu下的通配符主要有三个 1.*  这个是匹配任意一个或多个字符 ab1.txt ab2.txt ab3.txt abc.txt 执行命令以及结果如下 zhangshuli@zhangshul ...

  7. eclipse- 智能提示设置

    最近自己ubuntu 下的eclipse没办法只能提示了.后来在网上查了方法,完美解决了问题 1.java代码编辑的时候不提示 具体如下 Windows→Preferences→Java→Editor ...

  8. java创建节点和单向链表

    package datastructure; public class Node { private Object data; private Node next; public Node() { t ...

  9. 自定义Base 64加密

    一.前言 最近做软件需要一个功能,就是对文件进行加密.本来嘛,加密算法一堆一堆的,但是试了几个成熟的加密算法后发现对文件进行加密需要的时间很长,特别是上G的文件,这样客户是接受不了的.最后没办法了,好 ...

  10. 【hdu 4333】Revolving Digits

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=4333 [题意] 就是给你一个数字,然后把最后一个数字放到最前面去,经过几次变换后又回到原数字,问在这些数 ...