Android 布局之GridLayout


1 GridLayout简介

GridLayout是Android4.0新提供的网格矩阵形式的布局控件。

GridLayout的继承关系如下:
java.lang.Object
  --> android.view.View
    --> android.view.ViewGroup
      --> android.widget.GridLayout

GridLayout包含的属性如下

android:alignmentMode
属性说明:当设置alignMargins,使视图的外边界之间进行校准。可以取以下值:
alignBounds -- 对齐子视图边界。
alignMargins -- 对齐子视图边距。

android:columnCount
属性说明:GridLayout的最大列数

android:rowCount
属性说明:GridLayout的最大行数

android:columnOrderPreserved
属性说明: 当设置为true,使列边界显示的顺序和列索引的顺序相同。默认是true。

android:orientation
属性说明:GridLayout中子元素的布局方向。有以下取值:
horizontal -- 水平布局。
vertical -- 竖直布局。

android:rowOrderPreserved
属性说明: 当设置为true,使行边界显示的顺序和行索引的顺序相同。默认是true。

android:useDefaultMargins
属性说明: 当设置ture,当没有指定视图的布局参数时,告诉GridLayout使用默认的边距。默认值是false。

这些是GridLayout布局本身的属性。


2 GridLayout子元素属性

上面描述的 GridLayout 的属性,是 GridLayout 布局本身的属性;下面 GridLayout 布局中的元素所支持的属性。GridLayout 布局中的元素的属性,定义在 GridLayout.LayoutParams 中。取值如下:

2.1 android:layout_column

属性说明: 显示该空间的列。例如,android:layout_column="0",表示在第1列显示该控件;android:layout_column="1",表示在第2列显示该控件。

layout文件示例,

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:orientation="horizontal"
  6. android:rowCount="2"
  7. android:columnCount="3" >
  8.   <Button
  9. android:id="@+id/one"
  10. android:layout_column="1"
  11. android:text="1"/>
  12.   <Button
  13. android:id="@+id/two"
  14. android:layout_column="0"
  15. android:text="2"/>
  16.    <Button
  17. android:id="@+id/three"
  18. android:text="3"/>
  19.   <Button
  20. android:id="@+id/devide"
  21. android:text="/"/>
  22.  
  23. </GridLayout>

对应的显示效果图

layout文件说明
android:orientation="horizontal" -- GridLayout中控件的布局方向是水平布局。
android:rowCount="2"               -- GridLayout最大的行数为2行。
android:columnCount="3"          -- GridLayout最大的列数为3列。
android:layout_column="1"        -- 定义控件one的位于第2列。
android:layout_column="0"        -- 定义该控two件的位于第1列。

2.2 android:layout_columnSpan

属性说明: 该控件所占的列数。例如,android:layout_columnSpan="2",表示该控件占2列。

layout文件示例

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:orientation="horizontal"
  6. android:rowCount="2"
  7. android:columnCount="3" >
  8.   <Button
  9. android:id="@+id/one"
  10. android:layout_column="0"
  11. android:layout_columnSpan="2"
  12. android:text="1"/>
  13.   <Button
  14. android:id="@+id/two"
  15. android:text="2"/>
  16.    <Button
  17. android:id="@+id/three"
  18. android:text="3"/>
  19.   <Button
  20. android:id="@+id/devide"
  21. android:text="/"/>
  22.  
  23. </GridLayout>

对应的显示效果图

layout文件说明

数字"1"实际上占据的空间大小是2列,但是第2列显示为空白。若要第2列不显示空白,需要设置 android:layout_gravity属性,参考下例。

2.3 android:layout_row

属性说明: 该控件所在行。例如,android:layout_row="0",表示在第1行显示该控件;android:layout_row="1",表示在第2行显示该控件。它和 android:layout_column类似。

2.4 android:layout_rowSpan

属性说明: 该控件所占的行数。例如,android:layout_rowSpan="2",表示该控件占2行。它和 android:layout_columnSpan类似。

2.5 android:layout_gravity

属性说明

该控件的布局方式。可以取以下值:
  top                      -- 控件置于容器顶部,不改变控件的大小。
  bottom                -- 控件置于容器底部,不改变控件的大小。
  left                     -- 控件置于容器左边,不改变控件的大小。
  right                   -- 控件置于容器右边,不改变控件的大小。
  center_vertical     -- 控件置于容器竖直方向中间,不改变控件的大小。
  fill_vertical          -- 如果需要,则往竖直方向延伸该控件。
  center_horizontal -- 控件置于容器水平方向中间,不改变控件的大小。
  fill_horizontal      -- 如果需要,则往水平方向延伸该控件。
  center                -- 控件置于容器中间,不改变控件的大小。
  fill                     -- 如果需要,则往水平、竖直方向延伸该控件。
  clip_vertical        -- 垂直剪切,剪切的方向基于该控件的top/bottom布局属性。若该控件的gravity是竖直的:若它的gravity是top的话,则剪切该控件的底部;若该控件的gravity是bottom的,则剪切该控件的顶部。
  clip_horizontal     -- 水平剪切,剪切的方向基于该控件的left/right布局属性。若该控件的gravity是水平的:若它的gravity是left的话,则剪切该控件的右边;若该控件的gravity是  right的,则剪切该控件的左边。
  start                  -- 控件置于容器的起始处,不改变控件的大小。
  end                   -- 控件置于容器的结束处,不改变控件的大小。

对应函数: setGravity(int)

layout文件示例:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:orientation="horizontal"
  6. android:rowCount="2"
  7. android:columnCount="3" >
  8.   <Button
  9. android:id="@+id/one"
  10. android:layout_column="0"
  11. android:layout_columnSpan="2"
  12. android:layout_gravity="fill"
  13. android:text="1"/>
  14.   <Button
  15. android:id="@+id/two"
  16. android:text="2"/>
  17.    <Button
  18. android:id="@+id/three"
  19. android:text="3"/>
  20.   <Button
  21. android:id="@+id/devide"
  22. android:text="/"/>
  23.  
  24. </GridLayout>

对应的显示效果图


3 应用示例

定义一个简单的计算器界面,包含“0-9、.、+、-、*、/、=、”。用GridLayout实现。

layout文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- GridLayout: 5行 4列 水平布局 -->
  3. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:orientation="horizontal"
  7. android:rowCount="5"
  8. android:columnCount="4" >
  9.   <Button
  10. android:id="@+id/one"
  11. android:text="1"/>
  12.   <Button
  13. android:id="@+id/two"
  14. android:text="2"/>
  15.    <Button
  16. android:id="@+id/three"
  17. android:text="3"/>
  18.   <Button
  19. android:id="@+id/devide"
  20. android:text="/"/>
  21.   <Button
  22. android:id="@+id/four"
  23. android:text="4"/>
  24.   <Button
  25. android:id="@+id/five"
  26. android:text="5"/>
  27.   <Button
  28. android:id="@+id/six"
  29. android:text="6"/>
  30.   <Button
  31. android:id="@+id/multiply"
  32. android:text="×"/>
  33.   <Button
  34. android:id="@+id/seven"
  35. android:text="7"/>
  36.   <Button
  37. android:id="@+id/eight"
  38. android:text="8"/>
  39.   <Button
  40. android:id="@+id/nine"
  41. android:text="9"/>
  42. <Button
  43. android:id="@+id/minus"
  44. android:text="-"/>
  45. <Button
  46. android:id="@+id/zero"
  47. android:layout_columnSpan="2"
  48. android:layout_gravity="fill"
  49. android:text="0"/>
  50.   <Button
  51. android:id="@+id/point"
  52. android:text="."/>
  53. <Button
  54. android:id="@+id/plus"
  55. android:layout_rowSpan="2"
  56. android:layout_gravity="fill"
  57. android:text="+"/>
  58. <Button
  59. android:id="@+id/equal"
  60. android:layout_columnSpan="3"
  61. android:layout_gravity="fill"
  62. android:text="="/>
  63. </GridLayout>

点击下载:源代码

效果图:

本文转自:Android 布局之GridLayout

GridLayout: GridLayout使用简介(转)的更多相关文章

  1. GridLayout: GridLayout中Spec属性

    如果想要让GridLayout中的子元素能够平均分配,就需要用到 android:layout_columnWeight="1" android:layout_rowWeight= ...

  2. Android 布局之GridLayout

    Android 布局之GridLayout 1 GridLayout简介 GridLayout是Android4.0新提供的网格矩阵形式的布局控件. GridLayout的继承关系如下:java.la ...

  3. Android 布局之GridLayout(转载)

    转载:http://www.cnblogs.com/skywang12345/p/3154150.html 1 GridLayout简介 GridLayout是Android4.0新提供的网格矩阵形式 ...

  4. android——学习:网格布局——GridLayout

    Android一开始就提供了几种布局控件,如线性布局LinearLayout.相对布局RelativeLayout和表格布局TableLayout等,但在很多情况下,这些布局控件是不能满足要求的,因此 ...

  5. Android UI -- 布局介绍(布局包括FrameLayout, LinearLayout, RelativeLayout, GridLayout)

    首先介绍常用布局类 FrameLayout 最简单的布局管理器. 这个布局管理类有几个特性: 添加组件默认在左上角的. 如果添加多个组件会叠加到一起,并且都在左上角.(可以通过一gravity属性改变 ...

  6. android 05 桢布局:FrameLayout 网格布据 GridLayout

    xml文件: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android: ...

  7. SWT中的GridLayout(转)例子不错

    GridLayout 是一个非常强大的布局管理器,它可以实现很多复杂的布局,名字中暗示它将所有控件放置在类似网格的布局中.^__^GridLayout 有两个构造函数. GridLayout的构造函数 ...

  8. Swing-布局管理器之GridLayout(网格布局)-入门

    注:本文内容源自于三十一.Java图形化界面设计——布局管理器之GridLayout(网格布局),笔者在学习过程中根据自身理解修改了部分代码. 网格布局特点: l  使容器中的各组件呈M行×N列的网格 ...

  9. 【转】 Pro Android学习笔记(二八):用户界面和控制(16):GridLayout

    网格布局:GridLayout 我个人觉得GridLayout的设计还不很完善,每个网格的大小,由填充的cell决定,即默认是wrap很容易整个GridLayout超出屏幕.下面是一个例子: < ...

随机推荐

  1. 加标签的continue用法

    1.加标签的continue,类似于C语言的goto语句

  2. JSONObject对象

    1.JSONObject介绍 JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换的包. 方法: 的getString() ...

  3. python3_pygame游戏窗口创建

    python3利用第三方模块pygame创建游戏窗口 步骤1.导入pygame模块 步骤2.初始化pygame模块 步骤3.设置游戏窗口大小 步骤4.定义游戏窗口背景颜色 步骤5.开始循环检测游戏窗口 ...

  4. P1072 Hankson 的趣味题[数论]

    题目描述 Hanks 博士是 BT(Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲解了 ...

  5. Union-Find(并查集): Union-Find Application

    Union-find 可以应用在很多方面 之前我们看到了union-find在dynamic connectivity上的应用,接下来介绍它在percolation上的应用. union-find在K ...

  6. Oracle中修改某个字段可以为空

    待修改字段假定为:shuifen 1.当该字段为空时,可直接修改: alter table reportqymx modify shuifen null; 2.当待修改字段不为空时:新增一列把要改变的 ...

  7. HTTP的幂等性

    转自: https://www.jianshu.com/p/234cf2e96832 理解HTTP幂等性基于HTTP协议的Web API是时下最为流行的一种分布式服务提供方式.无论是在大型互联网应用还 ...

  8. 项目后端 - 虚拟环境搭建 | pycharm使用虚拟环境

    虚拟环境的搭建 优点 1.使不同应用开发环境相互独立 2.环境升级不影响其他应用,也不会影响全局的python环境 3.防止出现包管理混乱及包版本冲突 windows 安装 # 建议使用pip3安装到 ...

  9. Go读写文件

    Go序列化和反序列化 package main import ( "bufio" "encoding/json" "fmt" "o ...

  10. React navtive

    http://www.linuxidc.com/Linux/2015-09/123239.htm