在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. 让执行程序引用特定目录下的Dll

    当写一个软件,特别是大型的软件,经常会引用一些第三方的类库,再加上一些自己的项目,如果这些Dll全都放在主目录下的话,会显得比较杂乱.我们希望将项目的类库分类成文件夹存放,这样才显得比较整洁. 解决方 ...

  2. 慕课网-安卓工程师初养成-4-14 Java 循环语句之多重循环

    来源:http://www.imooc.com/code/1497 循环体中包含循环语句的结构称为多重循环.三种循环语句可以自身嵌套,也可以相互嵌套,最常见的就是二重循环.在二重循环中,外层循环每执行 ...

  3. EB(存储单位)

    abbr.艾字节,1EB=1024PB 计算机的存储单位 位 bit (比特)(Binary Digits):存放一位二进制数,即 0 或 1,最小的存储单位. 字节 byte:8个二进制位为一个字节 ...

  4. C++中rapidxml用法及例子

    rapidxml是一个快速的xml库,比tinyxml快了50-100倍.本文给出创建.读取.写入xml的源码. 由于新浪博客不支持文本文件上传,在使用下面代码需要先下载 rapidxml,关于这个库 ...

  5. 【MariaDB】MariaDB的复制

    GTID的说明 官网:https://mariadb.com/kb/en/mariadb/global-transaction-id/ 官网:http://dev.mysql.com/doc/refm ...

  6. 在centos上安装mysql5.7的三种方法

    带OS信息的是已编译的二进制文件,不带OS信息的是源码包 mysql-5.7.14-linux-glibc2.5-x86_64.tar.gz 二进制包 mysql-5.5.51.tar.gz 源码包 ...

  7. My Sql 1067错误兼编码问题解决

    My Sql 大部分都是用绿色版(解压版) 然后注册服务 简单方便. 但是.配置文件头痛的一逼. 首先配置mysql的环境变量. mySQL 环境变量(我的电脑-右击属性-高级-环境变量) MYSQL ...

  8. WP8_ListBox的用法

    在Windows Phone 7 Tips (5) 中曾经提到,在Windows Phone 7 中页面的布局一般分为:Panoramic.Pivot.List和Full Screen.而通常List ...

  9. SQL表自连接用法

      一个表与自身进行连接,称为自连接 问题的提出:一个网友提出这样一个SQL题目,说自己想了很久没解决,我一看,这不是很简单吗 可是自己在查询分析器调试了半天原来问题并不是那不简单 有一个学生表,里面 ...

  10. netlink机制

    一.netlink机制简介 netlink是一种基于网络的机制,允许在内核内部以及内核与用户之间进行通信.正式定义见RFC3549.手册见netlink(3)和netlink(7).netlink(3 ...