【ALearning】第四章 Android Layout组件布局(一)
在本章中,我们将Android学习组件布局。在前面的章节,我们也开始使用LinearLayout布局。然后我们在布局文件更加具体的学习和理解,会。
Android的界面是有布局和组件协同完毕的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦。组件依照布局的要求依次排列。就组成了用户所看见的界面。
Android的五大布局各自是LinearLayout(线性布局)、FrameLayout(单帧布局)、RelativeLayout(相对布局)、AbsoluteLayout(绝对布局)和TableLayout(表格布局)。
1、线性布局(LinearLayout)
线性布局的形式能够分为两种,第一种横向线性布局 另外一种纵向线性布局,总而言之都是以线性的形式 一个个排列出来的,纯线性布局的缺点是非常不方便改动控件的显示位置,所以开发中常常会以线性布局与相对布局嵌套的形式设置布局。
2、帧布局(FrameLayout)
原理是在控件中绘制不论什么一个控件都能够被后绘制的控件覆盖,最后绘制的控件会盖住之前的控件。
3、绝对布局(AbsoluteLayout)
使用绝对布局能够设置随意控件的 在屏幕中 X Y 坐标点,和帧布局一样后绘制的控件会覆盖住之前绘制的控件。
4、相对布局(RelativeLayout)
相对布局是android布局中最为强大的。首先它能够设置的属性是最多了,其次它能够做的事情也是最多的。android手机屏幕的分辨率五花八门所以为了考虑屏幕自适应的情况所以在开发中建议大家都去使用相对布局 它的坐标取值范围都是相对的所以使用它来做自适应屏幕是正确的。
5、表格布局(TableLayout)
在表格布局中能够设置TableRow 能够设置 表格中每一行显示的内容 以及位置 ,能够设置显示的缩进。对齐的方式。
【博客专栏:http://blog.csdn.net/column/details/alearning.html】
LinearLayout (线性布局)
LinearLayout中的子元素属性android:layout_weight的作用是描写叙述该子元素在剩余空间中占有的大小比例,默认值就为0。
本例拓展的属性配置是:
- android:layout_weight 权重
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity" > <!-- 单个LinearLayout --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#99CCFF"
android:orientation="horizontal" > <TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:text="#99CCFF" />
</LinearLayout> <!-- 单个LinearLayout下包括两个水平平均分布的LinearLayout --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#CCFF66"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:text="#CCFF66" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF9900"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:text="#FF9900" />
</LinearLayout>
</LinearLayout> <!-- 以上两种情况的叠加 --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#CC99CC"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="120dp"
android:gravity="center"
android:text="#CC99CC" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CC9933"
android:orientation="horizontal" > <TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:text="#CC9933" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#99CCCC"
android:orientation="horizontal" > <TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:text="#99CCCC" />
</LinearLayout>
</LinearLayout>
</LinearLayout> </LinearLayout>
效果截图:
FrameLayout (帧布局)
- android: gravity 指定控件的基本位置
android:gravity="bottom|center"
【布局文件】activity_framelayoutt.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_height="match_parent"
android:background="#99CCFF"
android:layout_width="match_parent"
android:gravity="bottom|center"
android:text="第一层\n【#99CCFF】"/> <TextView android:layout_height="200dp"
android:background="#CC99CC"
android:layout_width="200dp"
android:gravity="bottom|center"
android:text="第二层\n【#CC99CC】"/> <TextView android:layout_height="100dp"
android:layout_width="100dp"
android:gravity="bottom|center"
android:background="#99CCCC"
android:text="第三层\n【#99CCCC】"/> </FrameLayout>
效果截图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFob2tpbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
AbsoluteLayout (绝对布局)
在此布局中的子元素的android:layout_x和android:layout_y属性将生效,用于描写叙述该子元素的坐标位置。屏幕左上角为坐标原点(0,0)。第一个0代表横坐标,向右移动此值增大,第二个0代表纵坐标,向下移动,此值增大。
在此布局中的子元素能够相互重叠。在实际开发中,通常不採用此布局格式,由于它的界面代码过于刚性,以至于有不能非常好的适配各种各样的终端设备。
- android:layout_x 指定控件的x轴的位置
- android:layout_y 指定控件的y轴的位置
<? xml version="1.0" encoding="utf-8"? >
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_x="30dp"
android:layout_y="30dp"
android:background="#009966"
android:text="#009966" /> <TextView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_x="90dp"
android:layout_y="80dp"
android:background="#CC6699"
android:text="#CC6699" /> <TextView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_x="30dp"
android:layout_y="180dp"
android:background="#3366CC"
android:text="#3366CC" /> <TextView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_x="180dp"
android:layout_y="90dp"
android:background="#FFCC99"
android:text="#FFCC99" /> <TextView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_x="240dp"
android:layout_y="200dp"
android:background="#FF6666"
android:text="#FF6666" /> </AbsoluteLayout>
效果截图:
參考资料
1、http://blog.chinaunix.net/uid-23544029-id-2985631.html
2、http://www.cnblogs.com/wisekingokok/archive/2011/08/23/2150452.html
3、http://blog.csdn.net/jzp12/article/details/7590591
【ALearning】第四章 Android Layout组件布局(一)的更多相关文章
- 【ALearning】第四章 Android Layout组件布局(二)
前面我们分别介绍和学习了LinearLayout(线性布局).FrameLayout(单帧布局)和AbsoluteLayout(绝对布局).这次我们要进行RelativeLayout(相对布局)和Ta ...
- 第四章Android移植环境搭建
第四章Android移植环境搭建 这一章主要学习如何搭建 Android 移植的环境.因为 Android 底层是基于 Linux 内核的,所以本章从交叉编译环境等嵌入式开发环境的搭建开始,介绍了 B ...
- 【Android开发日记】之入门篇(四)——Android四大组件之Activity
在Android中,无论是开发者还是用户,接触最多的就算是Activity.它是Android中最复杂.最核心的组件.Activity组件是负责与用户进行交互的组件,它的设计理念在很多方面都和Web页 ...
- 第四章 android 命名规范和编码规范
书里面讲的比较常见,单个人也是有不同的观点: 因为android绝大部分使用java开发的,因此java相关规范适用于android: Google Style: 英文地址:http://google ...
- [Learn AF3]第四章 App framework组件之Button
Button 组件名称:Button 是否js控件:否 使用说明:如果说panel组件是af3的“核心(heart of the ui)”,那么Button就是af中的五虎上将之 ...
- android layout的布局
1.android:layout_width.android:layout_heigth 表示控件的大小,长宽 2.andoid:layout_gravity .android:gravity表示控件 ...
- Android笔记(五十四) Android四大组件之一——ContentProvider(一)
ContentProvider提供数据 在Android中,他的每个应用都是相互独立的,各自运行在自己的Dalvik虚拟机中,但现实使用中常常需要在多个应用之间进行数据交换,例如发短信需要获取联系人中 ...
- 【Android开发日记】之入门篇(六)——Android四大组件之Broadcast Receiver
广播接受者是作为系统的监听者存在着的,它可以监听系统或系统中其他应用发生的事件来做出响应.如设备开机时,应用要检查数据的变化状况,此时就可以通过广播来把消息通知给用户.又如网络状态改变时,电量变化时都 ...
- Android学习(四) Layout五大布局
我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前, 视图都是由一个一个的组件构成的.组件就是我们常见的Button.TextEdit ...
随机推荐
- 【老鸟学算法】包含 min函数的栈设计——java实现
要求: 1. 定义栈的数据结构,要求添加一个 min函数,能够得到栈的最小元素. 2. 要求函数 min.push 以及 pop 的时间复杂度都是 O(1). 这是考验“栈”数据结构设计.众所周知,栈 ...
- BZOJ 2096([Poi2010]Pilots-单调队列-差值)
2096: [Poi2010]Pilots Time Limit: 30 Sec Memory Limit: 162 MB Submit: 190 Solved: 97 [ Submit][ ...
- 浙江大学PAT上机题解析之2-06. 数列求和
给定某数字A(1<=A<=9)以及非负整数N(0<=N<=100000),求数列之和S = A + AA + AAA + … + AA…A(N个A).例如A=1, N=3时,S ...
- Delphi接口的底层实现(接口在内存中仍然有其布局,它依附在对象的内存空间中,有汇编解释)——接口的内存结构图,简单清楚,深刻 good
引言 接口是面向对象程序语言中一个很重要的元素,它被描述为一组服务的集合,对于客户端来说,我们关心的只是提供的服务,而不必关心服务是如何实现的:对于服务端的类来说,如果它想实现某种服务,实现与该服务相 ...
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- 使用Boost库中的组件进行C++内存管理
C++标准库中的auto_ptr,智能指针,部分的解决了获取资源自动释放的问题 在Boost中,提供了6中智能指针:scoped_ptr, scoped_array, shared_ptr, shar ...
- MVC之国际化
MVC之国际化 前言 在项目中遇到国际化语言的问题是常有的事情,之前在做关于MVC国际化语言时,刚开始打算全部利用AngularJS来实现,但是渐渐发现对于页面Title难以去控制其语言转换,于是对于 ...
- R语言数据框行转列实例
目的:须要把数据框的行列进行转置 方法: # 原始数据框 > hrl_jd_mon 年份 一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月 1 2010年 51 ...
- virtenv 0.8.6 发布,虚拟桌面配置工具 - 开源中国社区
virtenv 0.8.6 发布,虚拟桌面配置工具 - 开源中国社区 virtenv 0.8.6 发布,virtenv 是一个用 QT4 开发的应用,用来配置和启动基于 LXC 的虚拟桌面环境.该容器 ...
- Android 的独特shell命令
Android本来就是一个linux操作系统,所以大部分都是linux的命令,如mkdir,ls,netstat,mount,ps 等,这里就不具体介绍了, 主要介绍几个Android特有的. get ...