经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
安卓之布局总结
来源:cnblogs  作者:四两逻辑  时间:2018/12/14 9:26:09  对本文有异议

 Adroid布局

有人形象地比喻,Android开发中的布局就相当于一栋建筑的外观架构。布局用得好,这栋建筑的外观才美观高大上。

 

Android布局管理器

Android布局管理器本身是一个界面控件,所有的布局管理器都是ViewGroup类的子类,都是可以当做容器类来使用的。因此一个布局管理器中可以嵌套其他的布局管理器。

 

这是谷歌上找的一张布局管理器层级图

 

每一个ViewGroup都可以嵌套其他的ViewGroup和View(视图)。一个ViewGroup的大小是相对的,它即可以是其他ViewGroup的父容器,也可以是其他ViewGroup的子容器。在Android中,ViewGroup指代的是布局管理器,也就是下面要讲的布局样式,View指代的是布局管理器中的一个个控件。在Android中,控件可以在XML文件中定义,也可以程序员根据自己的需要去定义一个类。本文重点先不讨论视图中的控件,还是回归到布局。

 

Android六大基本布局管理器分别是:

线性布局(LinearLayout)、表格布局(TableLayout)、网格布局(GridLayout)、相对布局(RelativeLayout)、绝对布局(AbsoluteLayout)、层布局(FrameLayout)

其中,表格布局是线性布局的子类。网格布局是android 4.0后新增的布局。

 

(1)线性布局

线性布局会将容器内的所有控件一个挨着一个地排列。

属性:

1. 排列方向

android:orienation = “ horizontal/vertical”

水平排列和垂直排列,Android中默认为垂直排列vertical

注意:默认情况下水平和垂直方向的排列只占一行,如果用android:layout_width来设定控件的宽度,如果控件宽度太大,超出屏幕的显示范围,屏幕是不会显示超出的范围的。

 

2. 对齐方式

用于控制元素(例如文字)在该控件里的显示位置。

属性值:

可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical

也可以同时使用两个属性值,中间用 | 竖线隔开

例如:

android:gravity = “buttom|center_horizontal”

如果定义在控件中的底部,垂直居中

 

举一个简单例子,我用LinearLayout线性布局来实现常用的计算器

前面四行都很容易理解,用一个Linearout来包裹4个button控件,并且设定排列方向为水平方向。这里只列出其中一行的布局代码,其他三行的代码与第一行几乎相同。

  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal" >
  5. <Button
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:text="mc" android:layout_weight="1">
  9. //layout_weight设定水平布局中控件的占比
  10. </Button>
  11. <Button
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:text="m+"
  15. android:layout_weight="1">
  16. </Button>
  17. <Button
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:text="m-"
  21. android:layout_weight="1">
  22. </Button>
  23. <Button
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:text="mr"
  27. android:layout_weight="1">
  28. </Button>
  29. </LinearLayout>

 

最关键的是下面两行,即用绿色框框住的那一部分控件如何布局。这里我使用布局控制器内部嵌套布局控制器的方法。首先将绿色框内部的控件分成三个层级(我分别用不同颜色标注出来了)。第一个层级是绿色框,包含两个两列,即两个红色框。第二个层级是红色框,每个红色框看成一个整体的列,第一个列是左边的红色框,其内部包含两个行;第二个列是右边的红色框,即“=”号,包含一个垂直布局的列,占位两行。再对左边的红色框进行第三层级的拆分。可以拆分成两行,每一行占3个位。

于是就有下面的代码:

  1. <!--绿色框-->
  2. <LinearLayout android:orientation="horizontal"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content">
  5. <!--红色框-->
  6. <LinearLayout android:orientation="vertical"
  7. android:layout_weight="3"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content">
  10. <!--蓝色框-->
  11. <LinearLayout android:orientation="horizontal"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content">
  14. <Button
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_weight="1"
  18. android:text="1"></Button>
  19. <Button
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_weight="1"
  23. android:text="2"></Button>
  24. <Button
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_weight="1"
  28. android:text="3"></Button>
  29. </LinearLayout>
  30. <!--蓝色框 -->
  31. <LinearLayout android:orientation="horizontal"
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content">
  34. <Button
  35. android:layout_width="0px"
  36. android:layout_height="wrap_content"
  37. android:layout_weight="2"
  38. android:text="0">
  39. </Button>
  40. <Button
  41. android:layout_width="0px"
  42. android:layout_height="wrap_content"
  43. android:layout_weight="1"
  44. android:text=".">
  45. </Button>
  46. </LinearLayout>
  47. </LinearLayout>
  48. <!--红色框,=号-->
  49. <LinearLayout android:orientation="vertical"
  50. android:layout_weight="1"
  51. android:layout_width="wrap_content"
  52. android:layout_height="match_parent">
  53. <Button
  54. android:layout_width="match_parent"
  55. android:layout_height="match_parent"
  56. android:text="=">
  57. </Button>
  58. </LinearLayout>
  59. </LinearLayout>

 

 

(2)相对布局

相对布局是最灵活的一种布局方式,可以相对父容器和相对与其他控件进行布局。

主要参数有:

1. 是否对齐父容器的语法格式为true/false:

例如:android:layout_alignParentLeft = “true”

2. 为于给定ID控件不同方位的语法格式:

例如:android:layout_above="@id/btn1"

@id/btn1 控件的ID必须是事前已经定义好的

 

android:layout_alignParentLeft                     该控件是否对齐父容器的左端

android:layout_alignParentRight                  该控件是否齐其父容器的右端

android:layout_alignParentTop                    该控件是否对齐父容器的顶部

android:layout_alignParentBottom              该控件是否对齐父容器的底部

android:layout_centerInParent                    该控件是否相对于父容器居中

android:layout_toLeftOf                              该控件位于给定ID控件的左方

android:layout_toRightOf                           该控件位于给定ID控件的右方

android:layout_above                                该控件位于给定ID控件的上方

android:layout_below                                该控件位于给定ID控件的下方

android:layout_centerHorizontal               该控件是否横向居中

android:layout_centerVertical                   该控件是否垂直居中

 

实例代码:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:background="@color/darkslategray" >
  5. <Button android:id="@+id/btn1"
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:layout_centerInParent="true"
  9. android:layout_centerHorizontal="true"
  10. android:text="下">
  11. </Button>
  12. <Button android:id="@+id/btn2"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_toLeftOf="@id/btn1"
  16. android:layout_above="@id/btn1"
  17. android:text="左">
  18. </Button>
  19. <Button android:id="@+id/btn3"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_toRightOf="@id/btn1"
  23. android:layout_above="@id/btn1"
  24. android:text="右">
  25. </Button>
  26. <Button android:id="@+id/btn4"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_toRightOf="@id/btn2"
  30. android:layout_above="@id/btn2"
  31. android:background="@color/white"
  32. android:text="上">
  33. </Button>
  34.  
  35. </RelativeLayout>

 

效果图

 

 

(3)表格布局

表格布局是最规整的一种布局方式。想象一下EXCEL表格规整的行和列,android布局管理器中的TableLayout与EXCEL中的单元格有不少相似之处。如果学过HTML,用过tbale,tr,td标签,应该也会对下面的用法有更好的理解。

表格布局由一个TableLayout包裹起来,内部是一行行的控件,每一行用一个TableRow来包裹,每一行的元素个数(即列数)都是可以不同的。默认情况下,一行中的所有控件是等宽的。

 

控件属性,在TableLayout中定义,对所有行起作用:

1. android:collapseColumns=””         #指定被隐藏的列的序号

2. android:shrinkColumns=””            #指定被收缩的列的列序号

3. android:stretchColumns=””           #指定被拉伸的列的列序号

4. android:layout_column="3":        #表示跳过第三个控件,直接显示下一个控件,注意:这个属性从1开始计数

5. android:layout_span="3"             #表示合并3个单元格,也就说这个组件占3个单元格

 

注意:上面属性的使用场合,是在TableLayout还是在TableRow中使用;如果是在TableRow中使用,需要在TableRow的子控件中添加属性。前四个属性都是添加在TableLayout中的,最是添加在TableRow的子控件中。

 

实例:

就以我们平常用的日历为案例(由于屏幕太小,放不下最后一列星期六的日期)

 

实例代码:

由于以下代码有很多相似之处,我只截取了比较重要的一部分

1. 总体框架

  1. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/TableLayout2"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content" >
  6. <TableRow>
  7. <Button
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="周日"
  11. android:background="#00000000"/> #去除button控件的样式
  12. <Button
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="周一"
  16. android:background="#00000000"/>
  17.  
  18. <Button
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="周二"
  22. android:background="#00000000"/>
  23.  
  24. <Button
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="周三"
  28. android:background="#00000000" />
  29.  
  30. <Button
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:text="周四"
  34. android:background="#00000000"/>
  35.  
  36. <Button
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:text="周五"
  40. android:background="#00000000" />
  41. <Button
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="周六"
  45. android:background="#00000000" />
  46. </TableRow>
  47. <TableRow>
  48. ... ...
  49. </TableRow>
  50. ... ...
  51. ... ...
  52. </TableLayout>

 

2. 最后一行“不显示日期”的合并单元格样式

  1. <Button
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:text="30" />
  5. <Button
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:layout_span="2"
  9. android:background="#ffffff"
  10. android:text="不显示日期" />

 

 

(4)绝对布局

绝对布局指定每个控件在手机上的具体坐标,每个控件的位置和大小是固定的。由于不同手机屏幕大小可能不同,所以绝对布局只适用于固定的手机屏幕。平常用得比较少,这里就不做详细介绍了。

 

(5)层布局

层布局也叫帧布局。每个控件占据一层,后面添加的层会覆盖前面的层。如果后面的层的大小大于或者等于前面的层,那么前面的层就会被完全覆盖。后面层中的控件就看不到了。实际应用中如果想要得到一个空间浮现在另一个控件的上方,可以在控件内部嵌套层布局。

下面是层布局的原理图:

 

  1. 实例代码:
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/FrameLayout1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity"
  7. android:foregroundGravity="right|bottom">
  8.  
  9. <TextView
  10. android:layout_width="200dp"
  11. android:layout_height="200dp"
  12. android:background="#FF6143" /> //橙色
  13. <TextView
  14. android:layout_width="150dp"
  15. android:layout_height="150dp"
  16. android:background="#7BFE00" /> //绿色
  17. <TextView
  18. android:layout_width="100dp"
  19. android:layout_height="100dp"
  20. android:background="#FFFF00" /> //黄色
  21. </FrameLayout>

 

效果图,每一个textview都会被前一个textview覆盖:

 

实际应用:

在手机程序设计中,绝对布局基本上不用,用得相对较多的是线性布局相对布局。

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号