.Net码农学Android---五分钟了解布局
在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---五分钟了解布局的更多相关文章
- zookeeper-如何修改源码-《每日五分钟搞定大数据》
本篇文章仅仅是起一个抛砖迎玉的作用,举一个如何修改源码的例子.文章的灵感来自 ZOOKEEPER-2784. 提一个问题先 之前的文章讲过zxid的设计,我们先复习下: zxid有64位,分成两部分: ...
- .Net码农学Android---前言
自从毕业参加工作后,就一直想学移动领域得开发,但时间.精力.决心.学习成本等这些问题总在不同程度的阻碍着自己. 但这段时间自己想做一款属于自己的App的想法越来越强烈,我感到自己快压不住这股能量了.终 ...
- Android源码浅析(五)——关于定制系统,如何给你的Android应用系统签名
Android源码浅析(五)--关于定制系统,如何给你的Android应用系统签名 今天来点简单的我相信很多定制系统的同学都会有一些特定功能的需求,比如 修改系统时间 静默安装 执行某shell命令 ...
- Stream替代for-编码五分钟-划水五小时
Stream替代for-编码五分钟-划水五小时 天空没有痕迹,风雨已在心中. 背景:使用Stream 流式操作取代俄罗斯式套娃的for循环,解放底层劳动密集型码畜的双手,使编码五分钟划水五小时,不再是 ...
- Android五个布局
Android五大布局Layout 1,LinearLayout 线性布局(能够嵌套使用): 制定线性布局的排列方式:水平排列 horizontal.垂直排列 vertical eg: android ...
- 简单研究Android View绘制三 布局过程
2015-07-28 17:29:19 这一篇主要看看布局过程 一.布局过程肯定要不可避免的涉及到layout()和onLayout()方法,这两个方法都是定义在View.java中,源码如下: /* ...
- JVM内存管理------GC算法精解(五分钟让你彻底明白标记/清除算法)
相信不少猿友看到标题就认为LZ是标题党了,不过既然您已经被LZ忽悠进来了,那就好好的享受一顿算法大餐吧.不过LZ丑话说前面哦,这篇文章应该能让各位彻底理解标记/清除算法,不过倘若各位猿友不能在五分钟内 ...
- Android性能优化之布局优化
最新最准确内容建议直接访问原文:Android性能优化之布局优化 本文为Android性能优化的第二篇——布局优化,主要介绍使用抽象布局标签(include, viewstub, merge).去除不 ...
- Android 中常用的布局
一.线性布局----LinearLayout horizontal 水平 <?xml version="1.0" encoding="utf-8"?& ...
- android中5大布局
Android布局也可以用HTML5做,但是用户体验性差 Android布局里XML的属性配置 1. 五种Layout中Item的基础属性: layout_width & layout_hei ...
随机推荐
- Web自动化框架LazyUI使用手册(1)--框架简介
作者:cryanimal QQ:164166060 web端自动化简介 web端自动化,即通过自动化的方式,对Web页面施行一系列的仿鼠标键盘操作,以达到对Web页面的功能进行自动化测试的目的. 其一 ...
- 用github pages展示你的静态网页,多项目支持
我看到有分享用github pages来做博客的,不过我并不想挂博客在上面,我只是想将我的一些作品挂上去,然后链接到我的简历里,这样HR可以直接看到. 首先是最基本的操作,在github上创建一个新的 ...
- 代码生成器(CodeBuilder) 2 正式发布
CodeBuilder是一个通过获取数据库表和字段定义,通过模板转换生成三层结构.实体模型等代码的工具. CodeBuilder第一版距今已过去4个年头了,第一版做的功能繁多,体积庞大,但是用起来不太 ...
- canvas-7globleCompositeOperation2.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Markdown 编辑器语法指南
基本技巧 代码 如果你只想高亮语句中的某个函数名或关键字,可以使用 `function_name()` 实现 通常编辑器根据代码片段适配合适的高亮方法,但你也可以用 ``` 包裹一段代码,并指定一种语 ...
- LICEcap GIF 屏幕录制工具
LICEcap 是一款屏幕录制工具,支持导出git动画图片格式,简单好用.大小只有几百KB 运行之后,可以随意调整大小,右下角有开始/停止按钮. 压缩包:http://files.cnb ...
- java web学习
一直想下决心好好学下java web,但是总是间断,虽然我的方向是ios,但是觉得后台也是相当重要,毕竟移动端实际上更多也就是展示后台拉取到的信息,搞移动端的不能总是受制于后台,各位看官觉得呢? 这两 ...
- Linux下yum安装MPlayer 或 LVC视频播放器
添加第三方源 su -c 'rpm -ivh http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-stable.noar ...
- CSS3 图片悬浮缩放效果
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- wp8.1 全球化解决办法
最近在更新一个应用,在wp8.1里面重写整个应用,由于8.1版本的api.架构和windows8.1的接口高度相同,变化很大,在编码过程中,只能一边翻msdn资料一边摸索解决遇到的问题,其中程序标题和 ...