在Android中,共有五种布局方式,分别是:LinearLayout(线性布局),FrameLayout(帧布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),

TableLayout(表格布局);还有一种,是在Android4.0以后出现的新布局:GridLayout(网袋布局)。下面简单介绍一下每个布局的特点。

一.线性布局:简单来说,这个布局就像它的名字一样,里面所有的控件的摆放方式都是像直线一样的摆放方式。要么垂直,要么水平。垂直或水平可以通过设置

android:orientation="vertical"属性来确定控件的摆放方式;vertical代表垂直,horizontal代表水平。当垂直布局时,每一行就只有一个控件,多个控件依次垂直往下;

水平布局时,只有一行,每一个控件依次向右排列。

LinearLayout里面还有一个特别的属性:android:layout_weight="1",这是设置控件的权重。举个列子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="temp.com.androidtouch.MainActivity"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"/> </LinearLayout>

这是一个线性布局,里面控件的属性还没有给上权重,效果图如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUgAAAGoCAMAAAATjoyDAAAAAXNSR0IArs4c6QAAADNQTFRF8/PzOEir4eLoampqMD6f19fXvb29+/v7PlC1AAAA////Xmy/jJbMr7bgjI2N9vf5PDw80lcjKQAABqlJREFUeNrt3IFy2jgUBVAhYUOwJPz/X7uSoW3a2XYT1kmDfV4zA3FpG5+R37sYTUNQSimllFJvqNH5gQQJEiRIpZTSQwwbkCBBgnR+Sik9xLABCfKtf2svjx9je9x2fd4Vt3HIAPKPdc55Otyf5/Yc5CN1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件设置了权重之后:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="temp.com.androidtouch.MainActivity"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"
android:layout_weight="3"/> </LinearLayout>

效果图如下:

当我们设置了控件的权重之后,控件在整个布局里面所占的比例将会根据权重比来分配,权重是可以给小数的,但是我们基本上不会给。

权重是LinearLayout布局里的属性,其他的布局中不能使用。

二.帧布局:这是几个布局中最简单的一个布局,它会为每个添加进去的控件创建单独的帧,但是帧布局的摆放控件的方式是覆盖式的,就拿这段代码来说:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="temp.com.androidtouch.MainActivity"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"
android:layout_weight="3"/> </FrameLayout>

当我把布局改为帧布局时

两个按钮就会重合在一起,第二个按钮会覆盖在第一个按钮之上。这就是帧布局唯一的特点。

三.绝对布局: AbsoluteLayout这个布局方式很简单,主要属性就两个: layout_x 和layout_y       控件依靠这两个属性来定义自己在布局中的X坐标和Y坐标。 以屏幕左上角为(0,0)的坐标轴的x,y值,当向下或向右移动时,坐标值将变大。AbsoluteLayout允许元素之间互相重叠。我们通常不推荐使用 AbsoluteLayout,因为AbsoluteLayout写出来的布局在不同的设备上使用时会变形,然后布局就会很丑。

四.相对布局: RelativeLayout可以理解为某一个控件为参照物,来定位的布局方式,基本属性如下图:

五.表格布局:TableLayout类似Html里面的Table。每一个TableLayout里面有表格行TableRow,TableRow里面可以具体定义每一个控件。每个TableRow都会定义成一行。简单来说,TableLayout就是以一个<TableLayout></TableLayout>标签和多个<TableRow></TableRow>标签组成。布局里面有两个属性,一是 android:shrinkColumns="1"表示某列被收缩,另一个是android:stretchColumns="0"表示某列被拉伸,还有一个android:collapseColumns=""表示某列被隐藏。当然,在TableLayout里面可以合并行和和并列。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="表一 全局设置:列属性设置" /> <TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shrinkColumns="1"
android:stretchColumns="0"> <TableRow> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="该列可伸展" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="该列可收缩" />
</TableRow> <TableRow> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我像行方向伸展,我可以很长很长很长很长" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我像列方向收缩,我可以很短很短很短很短" />
</TableRow>
</TableLayout> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="表二 单元格设置:指定属性单元格" /> <TableLayout
android:layout_width="350dp"
android:layout_height="wrap_content"
android:shrinkColumns="0,2"
android:stretchColumns="1"
> <TableRow> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第0列" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第1列" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第2列" /> </TableRow> <TableRow> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="我被指定在第一列" />
</TableRow> <TableRow> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_span="2"
android:text="我跨1列和2列 不信你看"
/>
</TableRow>
</TableLayout> </LinearLayout>

这是一个包含TableLayout里基本的属性的列子,效果图如下:

五个布局简单介绍完了,就只剩下最后一个,网袋布局。网袋布局较为灵活,基本属性如下图:

再举个用GridLayout写的比较简单的例子:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4"
android:orientation="horizontal"
android:rowCount="6">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="." /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_rowSpan="2"
android:layout_gravity="fill_vertical"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:layout_columnSpan="3"
android:layout_gravity="fill_horizontal"/> </GridLayout>

效果图如下:

布局就做了下简单的介绍,深入了解还是的靠自己。

第一次写博客,有很多考虑不周的地方,望各位博友多多指教。

Android01-布局篇的更多相关文章

  1. react-native 之布局篇

    一.宽度单位和像素密度 react的宽度不支持百分比,设置宽度时不需要带单位,那么默认的单位是什么呢? /** * Sample React Native App * https://github.c ...

  2. 深入css布局篇(3)完结 — margin问题与格式化上下文

    深入css布局(3) - margin问题与格式化上下文      在css知识体系中,除了css选择器,样式属性等基础知识外,css布局相关的知识才是css比较核心和重要的点.今天我们来深入学习一下 ...

  3. 深入css布局篇(2) — 定位与浮动

    深入css布局(2) - 定位与浮动      在css知识体系中,除了css选择器,样式属性等基础知识外,css布局相关的知识才是css比较核心和重要的点.今天我们来深入学习一下css布局相关的知识 ...

  4. Angular Material 教程之布局篇

    Angular Material 教程之布局篇 (一) : 布局简介https://segmentfault.com/a/1190000007215707 Angular Material 教程之布局 ...

  5. 【WPF】 布局篇

    [WPF] 布局篇 一. 几个常用且至关重要的属性 1. Width,Height : 设置窗体,控件宽高. 这里注意,WPF是自适应的, 所以把这2个属性设置 Auto, 则控件宽高会自动改变. 2 ...

  6. 一步一步构建手机WebApp开发——页面布局篇

    继上一篇:一步一步构建手机WebApp开发——环境搭建篇过后,我相信很多朋友都想看看实战案例,这一次的教程是页面布局篇,先上图: 如上图所示,此篇教程便是教初学者如何快速布局这样的页面.废话少说,直接 ...

  7. 使用wepy开发微信小程序商城第三篇:购物车(布局篇)

    使用wepy开发微信小程序商城 第三篇:购物车(布局篇) 前两篇如下: 使用wepy开发微信小程序商城第一篇:项目初始化 使用wepy开发微信小程序商城第二篇:路由配置和页面结构 基于上两篇内容,开始 ...

  8. BFC之宽度自适应布局篇

    说到自适应布局,我们曾在“抛砖引玉之宽度自适应布局”一文中学习过.当时的核心思想主要是利用float+margin的形式.利用块状元素的流体特性,然后计算出float元素的宽度,并赋予到块状元素的相应 ...

  9. Zara带你快速入门WPF(2)---布局篇

    一.章节目标 这几章节我们会创建一个完整的Window程序,包括使用DataGrid空间,数据绑定是把.NET类中的数据提供给用户界面的一个重要概念,还允许修改数据,包括.NET4.5新增的INoti ...

  10. Android之微信布局篇

    一.准备工作: 1. 下载好相关的图片: 2.创建一个名WeiChat的项目,将图片复制到res----->drawable-hdpi目录下. 二.编写代码: 1. 最终效果: 2.微信可划分为 ...

随机推荐

  1. WebStorm ES6 语法支持设置&babel使用及自动编译

    一.语法支持设置 Preferences > Languages & Frameworks > JavaScript 二.Babel安装 1.全局安装 npm install -g ...

  2. TFLearn构建神经网络

    TFLearn构建神经网络 Building the network TFLearn lets you build the network by defining the layers. Input ...

  3. vue系列之动态路由【原创】

    开题 最近用vue来构建了一个小项目,由于项目是以iframe的形式嵌套在别的项目中的,所以对于登录的验证就比较的麻烦,索性后端大佬们基于现在的问题提出了解决的方案,在看到他们的解决方案之前,我先画了 ...

  4. SessionStateMode之SQL Server共享session

    分布式应用首先要解决的是跨域的问题,解决session.frame.cookie的跨域是最基本的,然后才是负载均衡和性能优化,上面的不解决就没法往后面进行.上一博客主要是解决了frame跨域的问题,今 ...

  5. 前端开发【第4篇:JavaScript基础】

    JavaScript简述 上一篇文章已经聊过JavaScript的历史了这里不再复述了直接切入正题,JavaScript是一门解释型.动态类型.弱类型语言. 解释型语言和编译型语言就类似看一本书,编译 ...

  6. javaScript数组去重方法

    在JAvascript平时项目开发中经常会用到数组去重的操作.这时候就要用到JS数组去重的方法了. demo1: 第一种:JS数组去重操作方法是利用遍历原数组,利用数组的indexOf()方法来来判断 ...

  7. 转:【Java并发编程】之二十二:并发新特性—障碍器CyclicBarrier(含代码)

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17512983 CyclicBarrier(又叫障碍器)同样是Java5中加入的新特性,使用 ...

  8. 团队作业8——第二次项目冲刺(Beta阶段)--第六天

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 学号 成员 贡献比 201421123001 廖婷婷 16% 201421123002 翁珊 16% 201421123004 ...

  9. 201521123091 《Java程序设计》第12周学习总结

    Java 第十一周总结 第十一周的作业. 目录 1.本章学习总结 2.Java Q&A 3.码云上代码提交记录及PTA实验总结 4.课后阅读 1.本章学习总结 1.1 以你喜欢的方式(思维导图 ...

  10. 201521123007《Java程序设计》第2周学习总结

    1.本周学习总结 类名第一个字母大写,类名下的方法如main第一个字母要小写: Java有三种基本数据类型:整型(byte,short,int,long,char),浮点型(float,double) ...