当前位置:   article > 正文

Android学习笔记 - 常用的布局_android布局方式有几种

android布局方式有几种

简介

Android中有六种的布局方式,分别是:LinearLayout(线性布局)、RelativeLayout(相对布局)、TableLayout(表格布局)、FrameLayout(帧布局)、AbsoluteLayout(绝对布局)、GridLayout(网格布局)。在开发中,我们用的最多的就是线性布局和相对布局。

LinearLayout(线性布局)

线性布局是我们在开发中用的最多的一种布局方式。我们在进行屏幕适配时用的比较多的就是LinearLayout的weight(权重属性)。

  1. 核心属性图

  1. weight(权重)属性详解

  • 用法

实现代码:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/LinearLayout1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="horizontal">
  7. <LinearLayout
  8. android:layout_width="0dp"
  9. android:layout_height="fill_parent"
  10. android:background="#ADFF2F"
  11. android:layout_weight="1"/>
  12. <LinearLayout
  13. android:layout_width="0dp"
  14. android:layout_height="fill_parent"
  15. android:background="#DA70D6"
  16. android:layout_weight="2"/>
  17. </LinearLayout>

用法归纳:

按比例划分水平方向,将涉及到的view的android:width的属性设置为0dp,然后设置android:weight属性设置比例即可。竖直方向需将android:height属性设置为0dp即可,然后设置android:weight即可。

Java代码中设置weight属性:

setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1))

但是使用LinearLayout布局仍然存在一些问题。当界面比较复杂的时候,使用多层LinearLayout进行嵌套就会降低渲染速度,而且如果是listView或GridView上的item时,效率会更低。另外太多层的LinearLayout嵌套会占用更多的系统资源,还有可能引发stackoverflow,这个时候就需要RelativeLayout(相对布局)。

RelativeLayout(相对布局)

  1. 核心属性图

  1. 父布局定位示意图

父容器定位示意图

  1. 兄弟布局组件定位

兄弟组件定位

关于兄弟组件定位最经典的例子就是"梅花布局"。

梅花布局

实现代码:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/RelativeLayout1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent" >
  6. <!-- 这个是在容器中央的 -->
  7. <ImageView
  8. android:id="@+id/img1"
  9. android:layout_width="80dp"
  10. android:layout_height="80dp"
  11. android:layout_centerInParent="true"
  12. android:src="@drawable/pic1"/>
  13. <!-- 在中间图片的左边 -->
  14. <ImageView
  15. android:id="@+id/img2"
  16. android:layout_width="80dp"
  17. android:layout_height="80dp"
  18. android:layout_toLeftOf="@id/img1"
  19. android:layout_centerVertical="true"
  20. android:src="@drawable/pic2"/>
  21. <!-- 在中间图片的右边 -->
  22. <ImageView
  23. android:id="@+id/img3"
  24. android:layout_width="80dp"
  25. android:layout_height="80dp"
  26. android:layout_toRightOf="@id/img1"
  27. android:layout_centerVertical="true"
  28. android:src="@drawable/pic3"/>
  29. <!-- 在中间图片的上面-->
  30. <ImageView
  31. android:id="@+id/img4"
  32. android:layout_width="80dp"
  33. android:layout_height="80dp"
  34. android:layout_above="@id/img1"
  35. android:layout_centerHorizontal="true"
  36. android:src="@drawable/pic4"/>
  37. <!-- 在中间图片的下面 -->
  38. <ImageView
  39. android:id="@+id/img5"
  40. android:layout_width="80dp"
  41. android:layout_height="80dp"
  42. android:layout_below="@id/img1"
  43. android:layout_centerHorizontal="true"
  44. android:src="@drawable/pic5"/>
  45. </RelativeLayout>
  1. margin与padding的区别

margin代表的是偏移,比如marginleft="5dp"表示组件离容器左边缘偏移5dp;而padding代表的则是填充,比如TextView中的文字,将textView的paddingleft="5dp",则是在组件里的元素的左边填充5dp的空间。 margin针对的是容器中的组件,而padding针对的是组件中的元素。

比较实例代码如下:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10. <Button
  11. android:id="@+id/btn1"
  12. android:layout_height="wrap_content"
  13. android:layout_width="wrap_content"
  14. android:text="Button"/>
  15. <Button
  16. android:paddingLeft="100dp"
  17. android:layout_height="wrap_content"
  18. android:layout_width="wrap_content"
  19. android:text="Button"
  20. android:layout_toRightOf="@id/btn1"/>
  21. <Button
  22. android:id="@+id/btn2"
  23. android:layout_height="wrap_content"
  24. android:layout_width="wrap_content"
  25. android:text="Button"
  26. android:layout_alignParentBottom="true"/>
  27. <Button
  28. android:layout_marginLeft="100dp"
  29. android:layout_height="wrap_content"
  30. android:layout_width="wrap_content"
  31. android:text="Button"
  32. android:layout_toRightOf="@id/btn2"
  33. android:layout_alignParentBottom="true"/>
  34. </RelativeLayout>

运行效果图:

TableLayout(表格布局)

  1. 介绍

在HTML中,我们可以通过< table >< tr >< td >等属性就可以生成一个HTML的表格, 而Android中也允许我们使用表格的方式来排列组件,就是行与列的方式,就说我们这节的TableLayout! 但却不像我们后面会讲到的Android 4.0后引入的GridLayout(网格)布局一样,直接就可以设置多少行与多少列。

  1. 核心属性图

  1. 如何确定行数和列数

  • 如果我们直接往TableLayout中添加组件的话,那么这个组件将占满一行!!!

  • 如果我们想一行上有多个组件的话,就要添加一个TableRow的容器,把组件都丢到里面!

  • tablerow中的组件个数就决定了该行有多少列,而列的宽度由该列中最宽的单元格决定

  • tablerow的layout_width属性,默认是fill_parent的,我们自己设置成其他的值也不会生效!!! 但是layout_height默认是wrapten——content的,我们却可以自己设置大小!

  • 整个表格布局的宽度取决于父容器的宽度(占满父容器本身)

  • 有多少行就要自己数啦,一个tablerow一行,一个单独的组件也一行!多少列则是看tableRow中 的组件个数,组件最多的就是TableLayout的列数

  1. 三个常用属性

  • 如果我们直接往TableLayout中添加组件的话,那么这个组件将占满一行!!!

  • 如果我们想一行上有多个组件的话,就要添加一个TableRow的容器,把组件都丢到里面!

  • tablerow中的组件个数就决定了该行有多少列,而列的宽度由该列中最宽的单元格决定

  • tablerow的layout_width属性,默认是fill_parent的,我们自己设置成其他的值也不会生效!!!

  • 但是layout_height默认是wrapten——content的,我们却可以自己设置大小!整个表格布局的宽度取决于父容器的宽度(占满父容器本身)

  • 有多少行就要自己数啦,一个tablerow一行,一个单独的组件也一行!多少列则是看tableRow中 的组件个数,组件最多的就是TableLayout的列数

  1. 三个常用的属性

  • android:collapseColumns:设置需要被隐藏的列的序号

  • android:shrinkColumns:设置允许被收缩的列的列序号

  • android:stretchColumns:设置运行被拉伸的列的列序号

以上三个属性的列号都是从0开始算的,比如collapseColumns = "1",对应的就是第2列。可以设置多个,使用","隔开,比如"0,2",如果是所有列都生效,则用"*"号即可。除了这三个常用属性,还有两个属性,分别就是跳格子以及合并单元格:

  • android:layout_column="2":表示的就是跳过第二个,直接显示到第三个格子处,从1开始算的

  • android:layout_span="4":表示合并4个单元格,也就说这个组件占4个单元格

属性使用实例:

①、collapseColumns(隐藏列)

在TableRow中定义5个按钮后,接着在最外层的TableLayout中添加以下属性: android:collapseColumns = "0,2"(隐藏第一与第三列)。

  1. <TableLayout
  2. android:id="@+id/TableLayout2"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:collapseColumns="0,2" >
  6. <TableRow>
  7. <Button
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="one" />
  11. <Button
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="two" />
  15. <Button
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="three" />
  19. <Button
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="four" />
  23. <Button
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="five" />
  27. </TableRow>
  28. </TableLayout>

效果图:

②、stretchColumns(拉伸列)

在TableLayout中设置了四个按钮,接着在最外层的TableLayout中添加以下属性: android:stretchColumns = "1"(设置第二列为可拉伸列,让该列填满这一行所有的剩余空间)

  1. <TableLayout
  2. android:id="@+id/TableLayout2"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:stretchColumns="1" >
  6. <TableRow>
  7. <Button
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="one" />
  11. <Button
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="two" />
  15. <Button
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="three" />
  19. <Button
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="four" />
  23. </TableRow>
  24. </TableLayout>

效果图:

③、shrinkColumns(收缩列)

设置了5个按钮和一个文本框,在最外层的TableLayout中添加以下属性: android:shrinkColumns = "1"(设置第二个列为可收缩列)

  1. <TableLayout
  2. android:id="@+id/TableLayout2"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:shrinkColumns="1" >
  6. <TableRow>
  7. <Button
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="one" />
  11. <Button
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="two" />
  15. <Button
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="three" />
  19. <Button
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="four" />
  23. <Button
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="five" />
  27. <TextView
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="文本XX" />
  31. </TableRow>
  32. </TableLayout>

效果图:

FrameLayout(帧布局)

  1. 介绍

FrameLayout(帧布局)是六个布局中最简单的布局,这个布局直接在屏幕上开辟出一块空白的区域,当我们往里面添加控件的时候,会默认把他们放到这块区域的左上角,而这种布局方式却没有任何的定位方式,所以它应用的场景并不多;帧布局的大小由控件中最大的子控件决定,如果控件的大小一样大的话,那么同一时刻就只能看到最上面的那个组件!后续添加的控件会覆盖前一个!虽然默认会将控件放置在左上角,但是我们也可以通过layout_gravity属性,指定到其他的位置!

  1. 常用属性

  • android:foreground:*设置改帧布局容器的前景图像

  • android:foregroundGravity:设置前景图像显示的位置

前景图片:永远处于帧布局最上面,直接面对用户的图像,就是不会被覆盖的图片。

  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:foreground="@drawable/logo"
  8. android:foregroundGravity="right|bottom">
  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>

效果图

GridLayout(网格布局)

  1. 核心属性图

  1. 使用实例

效果图:

实现代码:

  1. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/GridLayout1"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:columnCount="4"
  7. android:orientation="horizontal"
  8. android:rowCount="6" >
  9. <TextView
  10. android:layout_columnSpan="4"
  11. android:layout_gravity="fill"
  12. android:layout_marginLeft="5dp"
  13. android:layout_marginRight="5dp"
  14. android:background="#FFCCCC"
  15. android:text="0"
  16. android:textSize="50sp" />
  17. <Button
  18. android:layout_columnSpan="2"
  19. android:layout_gravity="fill"
  20. android:text="回退" />
  21. <Button
  22. android:layout_columnSpan="2"
  23. android:layout_gravity="fill"
  24. android:text="清空" />
  25. <Button android:text="+" />
  26. <Button android:text="1" />
  27. <Button android:text="2" />
  28. <Button android:text="3" />
  29. <Button android:text="-" />
  30. <Button android:text="4" />
  31. <Button android:text="5" />
  32. <Button android:text="6" />
  33. <Button android:text="*" />
  34. <Button android:text="7" />
  35. <Button android:text="8" />
  36. <Button android:text="9" />
  37. <Button android:text="/" />
  38. <Button
  39. android:layout_width="wrap_content"
  40. android:text="." />
  41. <Button android:text="0" />
  42. <Button android:text="=" />
  43. </GridLayout>

代码解析:只是"回退"与"清除"按钮横跨两列,而其他的都是直接添加的,默认每个组件都是 占一行一列,另外还有一点要注意的: 我们通过:android:layout_rowSpan与android:layout_columnSpan设置了组件横跨 多行或者多列的话,如果你要让组件填满横越过的行或列的话,需要添加下面这个属性: android:layout_gravity = "fill"

  1. 归纳

  1. 先定义组件的对其方式 android:orientation 水平或者竖直,设置多少行与多少列

  1. 设置组件所在的行或者列,记得是从0开始算的,不设置默认每个组件占一行一列

  1. 设置组件横跨几行或者几列;设置完毕后,需要在设置一个填充:android:layout_gravity = "fill"。

AbsoluteLayout(绝对布局)

  1. 介绍

AbsoluteLayout是直接通过X,Y坐标来 控制组件在Activity中的位置的!另外这个但单位是dp!

  1. 核心属性

①控制大小: android:layout_width:组件宽度

android:layout_height:组件高度

②控制位置: android:layout_x:设置组件的X坐标

android:layout_y:设置组件的Y坐标

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

闽ICP备14008679号