在android中应用的界面是以xml来组织的,这一点和WPF相似,通过配置xml文件我们可以灵活的构建出你自己想要的界面。

而在所有的xml界面文件中,根节点必须是布局,即先有布局,然后在布局中组织控件或嵌套布局,android中的布局有5种,熟悉各种布局的使用对我们以后开发中更好的组织界面大有益处,以下简单介绍。

  • TableLayout

表格布局,就是类似我们在网页中以表格来组织控件的布局。以N行N列的形式排列出相应的控件。

  <TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" > <TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1行1列" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1行2列" />
</TableRow> <TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2行1列" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2行2列" />
</TableRow> <TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3行1列" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3行2列" />
</TableRow>
</TableLayout>

  • LineLayout

线性布局,比较简单,就是以水平或垂直的方式一行一个或一列一个的形式摆放控件。

   <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >//水平或垂直 <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="1行" /> <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="2行" /> <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="3行" />
</LinearLayout>

  • RelativeLayout

相对布局,最为灵活得分一种布局,用于组织一些复杂界面,在此布局中的子元素里与位置相关的属性将生效。例如android:layout_below,  android:layout_above, android:layout_centerVertical等。注意在指定位置关系时,引用的ID必须在引用之前,先被定义,否则将出现异常。

  <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <Button
android:id="@+id/text_01"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="1" /> <Button
android:id="@+id/text_02"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="@id/text_01"//相对布局中的特有属性,xx之上/之下/之左/之右等
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="2" /> <Button
android:id="@+id/text_03"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="@id/text_01"
android:layout_toLeftOf="@id/text_02"//相对布局中的特有属性,在xx的左边
android:gravity="center"
android:text="3" />
</RelativeLayout>

  • AbsoluteLayout

绝对布局,在此布局中的子元素的android:layout_x和android:layout_y属性将生效,用于描述该子元素的坐标位置,一经设置变不能改变位置,适用一些不经常变化的控件或界面。

 <AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <Button
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="50dp"
android:layout_y="50dp"
android:background="#999999"
android:gravity="center"
android:text="1" /> <Button
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="90dp"
android:layout_y="90dp"
android:background="#ff654321"
android:gravity="center"
android:text="2" /> <Button
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="125dp"
android:layout_y="125dp"
android:background="#fffedcba"
android:gravity="center"
android:text="3" />
</AbsoluteLayout>

  • FrameLayout

帧布局,网上说的有些别扭,我自己理解就类似css中的z-index,可以实现遮罩,即有层的效果。注意:所有的帧默认都是从屏幕左上角开始绘制,然后按照控件的声明顺序,依次叠加,层级逐渐升高。

 <FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#999999"
android:gravity="center"
android:text="1" /> <TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#ff654321"
android:gravity="center"
android:text="2" /> <TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#fffedcba"
android:gravity="center"
android:text="3" />
</FrameLayout>

五大布局已经介绍完毕,有些简单,(因为这个布局我觉得确实也没什么好说的,结合配图我觉得已经很能说明问题了)。我在写这个系列之前也说了“我并不想做重复性的工作”,类似这种基础的东西,网上很多人写的比我好也很详细,我只是想把自己在学习过程中认为有必要和大家分享的一些经验和技巧写出来,所以你可以看到我并没有写什么HelloWorld之类的,因为与其把时间花在这种已经泛滥的内容上,倒不如多花时间去贡献一些独有的,或被大家忽略的内容上。

写技术博客真的很费时间,得理顺思路、组织语言、写Demo、截图、排版等等,有时候真羡慕那些可以一周一篇甚至几天一篇得大牛们。最近学的内容和点都比较多,看着大篇凌乱的笔记,一时间都不知道该从哪去写,自己更新的有些慢,不过自己还是会坚持的,一起加油!

.Net码农学Android---五分钟了解布局的更多相关文章

  1. zookeeper-如何修改源码-《每日五分钟搞定大数据》

    本篇文章仅仅是起一个抛砖迎玉的作用,举一个如何修改源码的例子.文章的灵感来自 ZOOKEEPER-2784. 提一个问题先 之前的文章讲过zxid的设计,我们先复习下: zxid有64位,分成两部分: ...

  2. .Net码农学Android---前言

    自从毕业参加工作后,就一直想学移动领域得开发,但时间.精力.决心.学习成本等这些问题总在不同程度的阻碍着自己. 但这段时间自己想做一款属于自己的App的想法越来越强烈,我感到自己快压不住这股能量了.终 ...

  3. Android源码浅析(五)——关于定制系统,如何给你的Android应用系统签名

    Android源码浅析(五)--关于定制系统,如何给你的Android应用系统签名 今天来点简单的我相信很多定制系统的同学都会有一些特定功能的需求,比如 修改系统时间 静默安装 执行某shell命令 ...

  4. Stream替代for-编码五分钟-划水五小时

    Stream替代for-编码五分钟-划水五小时 天空没有痕迹,风雨已在心中. 背景:使用Stream 流式操作取代俄罗斯式套娃的for循环,解放底层劳动密集型码畜的双手,使编码五分钟划水五小时,不再是 ...

  5. Android五个布局

    Android五大布局Layout 1,LinearLayout 线性布局(能够嵌套使用): 制定线性布局的排列方式:水平排列 horizontal.垂直排列 vertical eg: android ...

  6. 简单研究Android View绘制三 布局过程

    2015-07-28 17:29:19 这一篇主要看看布局过程 一.布局过程肯定要不可避免的涉及到layout()和onLayout()方法,这两个方法都是定义在View.java中,源码如下: /* ...

  7. JVM内存管理------GC算法精解(五分钟让你彻底明白标记/清除算法)

    相信不少猿友看到标题就认为LZ是标题党了,不过既然您已经被LZ忽悠进来了,那就好好的享受一顿算法大餐吧.不过LZ丑话说前面哦,这篇文章应该能让各位彻底理解标记/清除算法,不过倘若各位猿友不能在五分钟内 ...

  8. Android性能优化之布局优化

    最新最准确内容建议直接访问原文:Android性能优化之布局优化 本文为Android性能优化的第二篇——布局优化,主要介绍使用抽象布局标签(include, viewstub, merge).去除不 ...

  9. Android 中常用的布局

    一.线性布局----LinearLayout   horizontal 水平 <?xml version="1.0" encoding="utf-8"?& ...

  10. android中5大布局

    Android布局也可以用HTML5做,但是用户体验性差 Android布局里XML的属性配置 1. 五种Layout中Item的基础属性: layout_width & layout_hei ...

随机推荐

  1. Android开发-API指南-创建 Content Provider

    Creating a Content Provider 英文原文:http://developer.android.com/guide/topics/providers/content-provide ...

  2. 国内外CDN服务商CNAME特征串调研

    总结 此篇博文给特定需求的人群使用,通过CNAME的某些特征串,确定其使用的是哪家CDN,大多是国外的CDN,国内的CDN厂商只有几个,格式为:[来源地址]+[截图]+[猜测的特征串],整体博文较长, ...

  3. 多线程(三)GCD

    一.GCD GCD:Grand Central Dispatch,宏大的中央调度!!!是苹果为多核的并行运算提出的解决方案,会自动合理地利用更多的CPU内核(比如双核.四核), GCD的底层依然是用线 ...

  4. MSP430F149学习之路——AD

    代码一:Timer_A触发转换 #include <msp430x14x.h> void main() { WDTCTL = WDTPW + WDTHOLD; P6SEL |= BIT0; ...

  5. python和php对比

    对php比较熟悉,最近开始学些python,总是搞混,特记录下来,用于熟悉python:1.python数组和php不同,php相对简单统一,即array(包括普通数组和关联数组两部分),python ...

  6. codevs 1049 棋盘染色

    题目描述 Description 有一个5×5的棋盘,上面有一些格子被染成了黑色,其他的格子都是白色,你的任务的对棋盘一些格子进行染色,使得所有的黑色格子能连成一块,并且你染色的格子数目要最少.读入一 ...

  7. WWF3追踪功能<WWF第六篇>

    WWF工作流提供了Tracking跟踪功能来对工作流实例及其所包含的活动在运行时的状态进行跟踪,以便用户在需要时可以通过这些历史信息进行分析.WWF的Tracking跟踪功能是通过"SqlT ...

  8. solr5.5教程-tomcat布署(2)

    tomcat 布署成功后,接下来就是使用了. 首先要创建一个core. 1.选择右侧菜单, Core Admin -> Add Core. 注意:name自己定义,instanceDir要填写上 ...

  9. 【好文要转】Python:模拟登录以获取新浪微博OAuth的code参数值

    [转自]http://www.tuicool.com/articles/zAz6zi [原文]http://blog.segmentfault.com/hongfei/1190000000343851 ...

  10. Android SQLite数据储存方式

    SQLiteOpenHelper 类 用SQLiteOpenHelper 类中的 getWritableDatabase()和getReadableDatabase()方法可以获得数据库的引用. 为了 ...