前面我们分别介绍和学习了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. css3新特性选择器(补充)

    1.选择p标签中的第一个字符 p:first-letter{ color:red; font-size:25px; } 2.选择p标签中的第一行 p:first-line{ color:red; fo ...

  2. MyBatis映射

    mybatis-config.xml映射文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  3. 玲珑学院 1014 Absolute Defeat

    SAMPLE INPUT 3 2 2 2 1 1 5 1 4 1 2 3 4 5 4 10 3 1 2 3 4 SAMPLE OUTPUT 1 0 15 前缀和,每个元素都判断一下. #include ...

  4. deep-in-es6(一)

    一 迭代器和for-of循环 以前的一些遍历数组: function c(n) { console.log(n); } 方法一: for(let i = 0;i < arr.length;i++ ...

  5. 玩转Bash脚本:选择结构之case

    总第5篇 之前,我们谈到了if. 这次我们来谈还有一种选择结构--case. case与if if用于选择的条件,不是非常多的情况,假设选择的条件太多.一系列的if.elif,.也是醉了. 没错,ca ...

  6. android-Preference 风格调整

    <CheckBoxPreference android:defaultValue="false" android:layout="?android:attr/pre ...

  7. jQ-多选按钮实现单选按钮的功能以及input按钮的优化

    css: .displayN{display: none;} label {font-size:12px;cursor:pointer;} label i {font-size:12px;font-s ...

  8. Hadoop学习小结

    还在学校的时候,就知道Hadoop的存在了. 2012年在公司实习的时候,买了<Hadoop权威指南第2版>,大致看了下. 今年,抽空也大致喵了几眼. 最大的感悟就是:光看不做,还是不行. ...

  9. 5.9 enum--支持枚举类型

    enum模块提供了枚举类型的支持.枚举类型是由一个名称和一个统一值来组成.值是常量的值.它们之间能够通过名称进行比較和引用,还能够迭代訪问. 5.9.1 模块内容 本模块主要定义了两种枚举类型:Enu ...

  10. 1.3 Quick Start中 Step 2: Start the server官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 2: Start the server Step : 启动服务 Kafka ...