




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Android系统五大布局详解Layout 我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前, 视图都是由一个一个的组件构成的。组件就是我们常见的Button、TextEdit等等。那么我们平时看到的Android手机中那些漂亮的界面是怎么显示出来的呢?这就要用到Android的布局管理器了,网上有人比喻的很好:布局好比是建筑里的框架,组件按照布局的要求依次排列,就组成了用于看见的漂亮界面了。 在分析布局之前,我们首先看看控件
2、:Android中任何可视化的控件都是从android.veiw.View继承而来的,系统提供了两种方法来设置视图:第一种也是我们最常用的的使用XML文件来配置View的相关属性,然后在程序启动时系统根据配置文件来创建相应的View视图。第二种是我们在代码中直接使用相应的类来创建视图。 如何使用XML文件定义视图: 每个Android项目的源码目录下都有个res/layout目录,这个目录就是用来存放布局文件的。布局文件一般以对应activity的名字命名,以 .xml 为后缀。在xml中为创建组件时,需
3、要为组件指定id,如:android:id="+id/名字"系统会自动在gen目录下创建相应的R资源类变量。 如何在代码中使用视图: 在代码中创建每个Activity时,一般是在onCreate()方法中,调用setContentView()来加载指定的xml布局文件,然后就可以通过findViewById()来获得在布局文件中创建的相应id的控件了,如Button等。 如:private Button btnSndMag;public vo
4、id onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main);/ 加载main.xml布局文件 btnSndMag = (Button)this.findViewById(R.id.btnSndMag); / 通过id找到对于的Button组件 . 下面我们来介绍Android系统中为我们提供的五大布局:LinearLayout(线性布局)、FrameLayout(单帧布局)、AbsoluteLayout(绝对布
5、局)、TablelLayout(表格布局)、RelativeLayout(相对布局)。其中最常用的的是LinearLayout、TablelLayout和RelativeLayout。这些布局都可以嵌套使用。(1)LinearLayout 线性布局 线性布局是按照水平或垂直的顺序将子元素(可以是控件或布局)依次按照顺序排列,每一个元素都位于前面一个元素之后。线性布局分为两种:水平方向和垂直方向的布局。分别通过属性android:orientation="vertical" 和 android:orientation="horizontal"来
6、设置。 android:layout_weight 表示子元素占据的空间大小的比例,有人说这个值大小和占据空间成正比,有人说反比。我在实际应用中设置和网上资料显示的刚好相反,这个问题后面会专门写一篇文章来分析。现在我们只需要按照正比例来设置就可以。 例如下面我们实现一个如图所示的简易计算器界面:代码:<LinearLayout xmlns:android=" xmlns:tools=" android:orientation="vertical" android:layout_width="match_parent&q
7、uot; android:layout_height="match_parent" android:background="#FFFFFF" tools:context=".MainActivity" > / 这里第一行显示标签为一个水平布局 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizonta
8、l" > <EditText android:id="+id/msg" android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content" android:text=""> </EditText> </LinearLayout> / 第二行为 mc m+ m- mr 四个Button构成一个水平布局 <Lin
9、earLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="mc" android:layout_we
10、ight="1"> </Button> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="m+" android:layout_weight="1"> </Button> <Button android:layout_width="match_parent" android:layout_h
11、eight="wrap_content" android:text="m-" android:layout_weight="1"> </Button> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="mr" android:layout_weight="1"> </Button&g
12、t; </LinearLayout> / 同上 C +/- / * 四个Button构成一个水平布局 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="match_parent" android:layout_height="
13、wrap_content" android:layout_weight="1" android:text="C" > </Button> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="+/-" > </Button> <But
14、ton android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="/" > </Button> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_
15、weight="1" android:text="*" > </Button> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="match_parent&
16、quot; android:layout_height="wrap_content" android:text="7" android:layout_weight="1"> </Button> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="8" android:layout_weight="1&quo
17、t;> </Button> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="9" android:layout_weight="1"> </Button> <Button android:layout_width="match_parent" android:layout_height="wrap_
18、content" android:text="-" android:layout_weight="1"> </Button> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layo
19、ut_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" > </Button> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1&q
20、uot; android:text="5" > </Button> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="6" > </Button> <Button android:layout_width="match_parent" an
21、droid:layout_height="wrap_content" android:layout_weight="1" android:text="+" > </Button> </LinearLayout> / 最外层是一个水平布局,由左边上面一行1 2 3三个Button,下面一行的0 . 两个Button 和 右边的=构成 <LinearLayout android:orientation="horizontal" android:layout_width="m
22、atch_parent" android:layout_height="wrap_content"> / 这里 1 2 3 和 下面的 0 . 构成一个垂直布局 <LinearLayout android:orientation="vertical" android:layout_weight="3" android:layout_width="wrap_content" android:layout_height="wrap_content"> / 这里的 1 2
23、3 构成一个水平布局 <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="
24、1" android:text="1"></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2"></Button> <Button android:layout_width="wrap_content" and
25、roid:layout_height="wrap_content" android:layout_weight="1" android:text="3"></Button> </LinearLayout> / 这里的 0 和 . 构成一个水平布局,注意这里的android_weight参数设置 <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" a
26、ndroid:layout_height="wrap_content"> <Button android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="2" android:text="0"></Button> <Button android:layout_width="0px" android:layout_height=&qu
27、ot;wrap_content" android:layout_weight="1" android:text="."></Button> </LinearLayout> </LinearLayout> / 这里一个单独Button构成的垂直布局 <LinearLayout android:orientation="vertical" android:layout_weight="1" android:layout_width="wrap_cont
28、ent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="="></Button> </LinearLayout> </LinearLayout> </LinearLayout>(2)TableLayout 表格布局
29、; 表格布局,适用于多行多列的布局格式,每个TableLayout是由多个TableRow组成,一个TableRow就表示TableLayout中的每一行,这一行可以由多个子元素组成。实际上TableLayout和TableRow都是LineLayout线性布局的子类。但是TableRow的参数android:orientation属性值固定为horizontal,且android:layout_width=MATCH_PARENT,android:layout_height=WRAP_CONTENT。所以TableRow实际是一个横向的线
30、性布局,且所以子元素宽度和高度一致。 注意:在TableLayout中,单元格可以为空,但是不能跨列,意思是只能不能有相邻的单元格为空。 在TableLayout布局中,一列的宽度由该列中最宽的那个单元格指定,而该表格的宽度由父容器指定。可以为每一列设置以下属性: Shrinkable 表示该列的宽度可以进行收缩,以使表格能够适应父容器的大小 Stretchable 表示该列的宽度可以进行拉伸,以使能够
31、填满表格中的空闲空间 Collapsed 表示该列会被隐藏TableLayout中的特有属性:android:collapseColumns android:shrinkColumns android:stretchColumns = "0,1,2,3"Demo:我们想设计一个如下所以的一个三行三列的表格,但是第二行我们只想显示2个表格:<?xml version="1.0" encoding
32、="utf-8"?><TableLayout xmlns:android=" android:orientation="vertical" android:shrinkColumns="0,1,2" / 设置三列都可以收缩 android:stretchColumns="0,1,2" / 设置三列都可以拉伸 如果不设置这个,那个显示的表格将不能填慢整个屏幕 android:layout_width="fill_parent" android:layout_height=&q
33、uot;fill_parent" > <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:gravity="center" android:padding="10dp" android:text="Button1"> </Button> <Button android:gravity="
34、;center" android:padding="10dp" android:text="Button2"> </Button> <Button android:gravity="center" android:padding="10dp" android:text="Button3"> </Button> </TableRow> <TableRow android:layout_width="fill_paren
35、t" android:layout_height="wrap_content"> <Button android:gravity="center" android:padding="10dp" android:text="Button4"> </Button> <Button android:gravity="center" android:padding="10dp" android:text="Button5&quo
36、t;> </Button> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:gravity="center" android:padding="10dp" android:text="Button6"> </Button> <Button android
37、:gravity="center" android:padding="10dp" android:text="Button7"> </Button> <Button android:gravity="center" android:padding="10dp" android:text="Button8"> </Button> </TableRow></TableLayout>(3)RelativeLayout
38、 相对布局 RelativeLayout继承于android.widget.ViewGroup,其按照子元素之间的位置关系完成布局的,作为Android系统五大布局中最灵活也是最常用的一种布局方式,非常适合于一些比较复杂的界面设计。 注意:在引用其他子元素之前,引用的ID必须已经存在,否则将出现异常。常用的位置属性: android:layout_toLeftOf 该组件位于引用组件的左方android:layout_toRightOf 该组件位于引用组件的右方android:layout_above 该组件位于引用组件的上
39、方android:layout_below 该组件位于引用组件的下方android:layout_alignParentLeft 该组件是否对齐父组件的左端android:layout_alignParentRight 该组件是否齐其父组件的右端android:layout_alignParentTop 该组件是否对齐父组件的顶部android:layout_alignParentBottom 该组件是否对齐父组件的底部android:layout_centerInParent 该组件是否相对于父组件居中android:layout_centerHorizontal 该组件是否横向居中andr
40、oid:layout_centerVertical 该组件是否垂直居中Demo:利用相对布局设计一个如下图所示的界面:源码:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="+id/btn1&q
41、uot; android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_centerHorizontal="true" android:text="Button1" ></Button> <Button android:id="+id/btn2" android:la
42、yout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="id/btn1" android:layout_above="id/btn1" android:text="Button2" ></Button> <Button android:id="+id/btn3" android:layout_width="wrap_cont
43、ent" android:layout_height="wrap_content" android:layout_toRightOf="id/btn1" android:layout_above="id/btn1" android:text="Button3" ></Button> <Button android:id="+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="id/btn2" and
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 河南测绘职业学院《社会体育运动技能与指导(瑜伽)》2023-2024学年第二学期期末试卷
- 广西财经学院《流域管理学》2023-2024学年第一学期期末试卷
- 吉安职业技术学院《群落生态学》2023-2024学年第二学期期末试卷
- 重庆城市科技学院《安全行为学》2023-2024学年第二学期期末试卷
- 新疆农业大学《医学影像诊断学1》2023-2024学年第二学期期末试卷
- 海南大学《中国古文名篇导读》2023-2024学年第二学期期末试卷
- 安阳幼儿师范高等专科学校《学位论文选题与设计》2023-2024学年第二学期期末试卷
- 公章申请流程
- 抽排水施工方案
- 2025年乡村医生岗位理论知识考试模拟试题及答案(共100题)
- 2025广西中烟工业限责任公司招聘126人高频重点提升(共500题)附带答案详解
- 一体化指挥调度平台建设方案
- 人教版音乐教材培训
- 2025安徽合肥市轨道交通集团限公司社会招聘50人高频重点提升(共500题)附带答案详解
- 银行卡借给别人的授权委托书
- 工程送审金额超合同价10%的补充协议
- 2024年安徽省中考地理真题(原卷版)
- 模拟集成电路设计知到智慧树章节测试课后答案2024年秋广东工业大学
- 附件1:安全检查清单
- 医务人员生涯规划
- 《京剧欣赏》课件
评论
0/150
提交评论