当前位置:   article > 正文

Android五大布局Layout_android layout

android layout


Android开发中,我们可能会遇到过一些很复杂的布局,对于初学者来说,可能脑子会嗡的一下,“这么复杂!该怎么整?!”。

不要担心!再复杂的布局其实也是由简单地布局组成的,我们要学会将它分解成基本的布局,那么问题就迎刃而解了。

Android共有五种常见布局方式,分别是:LinearLayout(线性布局),FrameLayout(单帧布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局)。


下面首先看一下这些布局的关系

由此我们可以看出所有的布局方式都可以归类为ViewGroup的5个类别,即ViewGroup的5个直接子类。其它的一些布局都扩展自这5个类。

一、LinearLayout,线性布局方式

  这种布局比较常用,也比较简单,就是每个元素占一行,可声明为横向或纵向排放,也就是每个元素占一列。

  LinearLayout按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。如果是垂直排列,那么将是一个N行单列的结构,每一行只会有一个元素,而不论这个元素的宽度为多少;如果是水平排列,那么将是一个单行N列的结构。如果搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个LinearLayout进行水平排列。

  LinearLayout中的子元素属性android:layout_weight生效,它用于描述该子元素在剩余空间中占有的大小比例。加入一行只有一个文本框,那么它的默认值就为0,如果一行中有两个等长的文本框,那么他们的android:layout_weight值可以是同为1。如果一行中有两个不等长的文本框,那么他们的android:layout_weight值分别为1和2,那么第一个文本框将占据剩余空间的三分之一,第二个文本框将占据剩余空间中的三分之二。

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:orientation="vertical" >
  5. <TextView
  6. android:layout_width="fill_parent"
  7. android:layout_height="30dp"
  8. android:background="@android:color/holo_blue_bright" />
  9. <LinearLayout
  10. android:layout_width="fill_parent"
  11. android:layout_height="30dp"
  12. android:orientation="horizontal" >
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="fill_parent"
  16. android:layout_weight="0.2"
  17. android:background="@android:color/holo_green_dark" />
  18. <TextView
  19. android:layout_width="wrap_content"
  20. android:layout_height="fill_parent"
  21. android:layout_weight="0.3"
  22. android:background="@android:color/holo_orange_dark" />
  23. <TextView
  24. android:layout_width="wrap_content"
  25. android:layout_height="fill_parent"
  26. android:layout_weight="0.3"
  27. android:background="@android:color/holo_blue_light" />
  28. <!--
  29. android:layout_weight="0.3" 作用:子控件占父控件的比例,即0.3/(0.2+0.3+0.3),
  30. 值得注意的是如果有父布局中有一个子控件有weight值,那么其他同在父布局中的子控件都要有,否则就显示不出来。
  31. -->
  32. </LinearLayout>
  33. <LinearLayout
  34. android:layout_width="80dp"
  35. android:layout_height="60dp"
  36. android:layout_marginBottom="20dp"
  37. android:layout_marginLeft="20dp"
  38. android:background="@android:color/holo_red_dark" >
  39. <!-- 很容易看出 android:layout_marginBottom="20dp"(离底部20dp)没有起作用,因为LinearLayout是一个控件挨着一个布局的 -->
  40. </LinearLayout>
  41. </LinearLayout>
效果图:   



二、Relative Layout,相对布局

  RelativeLayout按照各子元素之间的位置关系完成布局。在此布局中的子元素里与位置相关的属性将生效。例如android:layout_below,  android:layout_above, android:layout_centerVertical等。注意在指定位置关系时,引用的ID必须在引用之前,先被定义,否则将出现异常。

  RelativeLayout是Android五大布局结构中最灵活的一种布局结构,比较适合一些复杂界面的布局。初学者可以利用可视化编辑器直接拖动控件到指定位置,来熟悉相关的位置属性。


  1. <RelativeLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextView
  6. android:id="@+id/a1"
  7. android:layout_width="50dp"
  8. android:layout_height="60dp"
  9. android:background="@android:color/holo_green_dark"/>
  10. <TextView
  11. android:id="@+id/a2"
  12. android:layout_width="70dp"
  13. android:layout_height="60dp"
  14. android:layout_alignParentTop="true"
  15. android:layout_marginLeft="36dp"
  16. android:layout_toRightOf="@+id/a1"
  17. android:background="@android:color/holo_blue_bright" />
  18. <TextView
  19. android:id="@+id/a3"
  20. android:layout_width="30dp"
  21. android:layout_height="60dp"
  22. android:layout_toEndOf="@id/a1"
  23. android:background="@android:color/holo_orange_dark" />
  24. <!-- layout_toEndOf和 layout_toRightOf有什么区别??-->
  25. <TextView
  26. android:id="@+id/a4"
  27. android:layout_width="70dp"
  28. android:layout_height="60dp"
  29. android:layout_marginLeft="36dp"
  30. android:layout_below="@id/a2"
  31. android:background="@android:color/black" />
  32. <TextView
  33. android:id="@+id/a5"
  34. android:layout_width="90dp"
  35. android:layout_height="30dp"
  36. android:layout_marginLeft="36dp"
  37. android:layout_alignParentRight="true"
  38. android:layout_alignParentBottom="true"
  39. android:background="@android:color/holo_purple" />
  40. </RelativeLayout>

效果图: 


三、AbsoluteLayout,绝对位置布局

  在此布局中的子元素的android:layout_x和android:layout_y属性将生效,用于描述该子元素的坐标位置。屏幕左上角为坐标原点(0,0),第一个0代表横坐标,向右移动此值增大,第二个0代表纵坐标,向下移动,此值增大。在此布局中的子元素可以相互重叠。在实际开发中,通常不采用此布局格式,因为它的界面代码过于刚性,以至于有可能不能很好的适配各种终端。

  1. <AbsoluteLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. >
  6. <TextView
  7. android:layout_width="90dp"
  8. android:layout_height="30dp"
  9. android:layout_x="0dp"
  10. android:layout_y="0dp"
  11. android:background="@android:color/holo_orange_dark"/>
  12. <TextView
  13. android:layout_width="90dp"
  14. android:layout_height="50dp"
  15. android:layout_x="100dp"
  16. android:layout_y="200dp"
  17. android:background="@android:color/holo_blue_light"/>
  18. <!-- 绝对布局是写死的坐标,不具有适配性,不建议使用 -->
  19. </AbsoluteLayout>

效果图:



四、FrameLayout,帧布局


  FrameLayout是五大布局中最简单的一个布局,可以说成是层布局方式。在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。并且android:gravity属性失效,但是可以通过layout_marginLeft等进行更改位置。

  1. <FrameLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. >
  6. <TextView
  7. android:layout_width="300dp"
  8. android:layout_height="300dp"
  9. android:background="@android:color/holo_blue_bright"/>
  10. <TextView
  11. android:layout_width="200dp"
  12. android:layout_height="200dp"
  13. android:background="@android:color/holo_green_light"/>
  14. <TextView
  15. android:layout_width="100dp"
  16. android:layout_height="100dp"
  17. android:layout_marginLeft="60dp"
  18. android:gravity="bottom"
  19. android:background="@android:color/holo_red_dark"/>
  20. <!-- gravity属性无效 -->
  21. </FrameLayout>

效果图: 


五、TableLayout,表格布局

   适用于N行N列的布局格式。一个TableLayout由许多TableRow组成,一个TableRow就代表TableLayout中的一行。

  TableRow是LinearLayout的子类,ablelLayout并不需要明确地声明包含多少行、多少列,而是通过TableRow,以及其他组件来控制表格的行数和列数, TableRow也是容器,因此可以向TableRow里面添加其他组件,没添加一个组件该表格就增加一列。如果想TableLayout里面添加组件,那么该组件就直接占用一行。在表格布局中,列的宽度由该列中最宽的单元格决定,整个表格布局的宽度取决于父容器的宽度(默认是占满父容器本身)。

  TableLayout继承了LinearLayout,因此他完全可以支持LinearLayout所支持的全部XML属性,除此之外TableLayout还支持以下属性:

       XML属性                                    相关用法                                         说明

  1. andriod:collapseColumns            setColumnsCollapsed(int ,boolean)                     设置需要隐藏的列的序列号,多个用逗号隔开

  2.android:shrinkColumns              setShrinkAllColumns(boolean)                          设置被收缩的列的序列号,多个用逗号隔开

  3.android:stretchColimns             setSretchAllColumnds(boolean)                         设置允许被拉伸的列的序列号,多个用逗号隔开


  1. <TableLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. >
  6. <TableRow
  7. android:id="@+id/tableRow1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content" >
  10. <TextView
  11. android:layout_width="80dp"
  12. android:layout_height="40dp"
  13. android:background="@android:color/holo_blue_dark"/>
  14. <TextView
  15. android:layout_width="80dp"
  16. android:layout_height="40dp"
  17. android:background="@android:color/holo_red_dark"/>
  18. </TableRow>
  19. <TableRow
  20. android:id="@+id/tableRow2"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:orientation="vertical">
  24. <TextView
  25. android:layout_width="200dp"
  26. android:layout_height="40dp"
  27. android:background="@android:color/holo_red_dark"/>
  28. <TextView
  29. android:layout_width="10dp"
  30. android:layout_height="40dp"
  31. android:background="@android:color/holo_green_dark"/>
  32. </TableRow>
  33. <TableRow
  34. android:id="@+id/tableRow3"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content" >
  37. <TextView
  38. android:layout_width="80dp"
  39. android:layout_height="40dp"
  40. android:background="@android:color/holo_green_dark"/>
  41. </TableRow>
  42. <TableRow
  43. android:id="@+id/tableRow4"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content" >
  46. <TextView
  47. android:layout_width="80dp"
  48. android:layout_height="40dp"
  49. android:background="@android:color/holo_blue_dark"/>
  50. </TableRow>
  51. <!-- 列的宽度为什么是一致的??? -->
  52. </TableLayout>

效果图:(红色的实际尺寸)



demo地址:http://download.csdn.net/detail/u011747781/8481315

Android布局文件Layout.xml的一些常见属性值:http://blog.csdn.net/google_acmer/article/details/44119741















声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/270405
推荐阅读
相关标签
  

闽ICP备14008679号