常用属性:

排列对齐:

①设置组件的排列方式:  android:orientation=""     vertical(竖直,默认)或者horizontal(水平)

②设置组件的对齐方式:  android:layout_gravity=""  center,left,right,buttom啊,这些,如果想同时用两种的话:eg: buttom|left


设置布局为几行几列:

①设置有多少行:android:rowCount="4"       //设置网格布局有4行

②设置有多少列:android:columnCount="4"    //设置网格布局有4列


设置某个组件位于几行几列

注:都是从0开始算的哦!

①组件在第几行:android:layout_row = "1"   //设置组件位于第二行

②组件在第几列:android:layout_column = "2"   //设置该组件位于第三列

设置某个组件横跨几行几列:

①横跨几行:android:layout_rowSpan = "2"    //纵向横跨2行

②横跨几列:android:layout_columnSpan = "3"     //横向横跨2列

使用实例:

最最最普遍的例子----计算器界面:

效果图:

PS:这里要说一点,网格布局和其他布局不同,可以不为组件设置Layout_width和Layout_height属性

因为组件的宽高由几行几列决定了,当然,你也可以写个wrap_content

代码:

  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:rowCount="6"
  7. android:columnCount="4"
  8. android:orientation="horizontal">
  9. <TextView
  10. android:layout_columnSpan="4"
  11. android:text="0"
  12. android:textSize="50sp"
  13. android:layout_marginLeft="5dp"
  14. android:layout_marginRight="5dp"
  15. />
  16. <Button
  17. android:text="回退"
  18. android:layout_columnSpan="2"
  19. android:layout_gravity="fill"
  20. />
  21. <Button
  22. android:text="清空"
  23. android:layout_columnSpan="2"
  24. android:layout_gravity="fill"
  25. />
  26. <Button
  27. android:text="+"
  28. />
  29. <Button
  30. android:text="1"
  31. />
  32. <Button
  33. android:text="2"
  34. />
  35. <Button
  36. android:text="3"
  37. />
  38. <Button
  39. android:text="-"
  40. />
  41. <Button
  42. android:text="4"
  43. />
  44. <Button
  45. android:text="5"
  46. />
  47. <Button
  48. android:text="6"
  49. />
  50. <Button
  51. android:text="*"
  52. />
  53. <Button
  54. android:text="7"
  55. />
  56. <Button
  57. android:text="8"
  58. />
  59. <Button
  60. android:text="9"
  61. />
  62. <Button
  63. android:text="/"
  64. />
  65. <Button
  66. android:layout_width="wrap_content"
  67. android:text="."
  68. />
  69. <Button
  70. android:text="0"
  71. />
  72. <Button
  73. android:text="="
  74. />
  75. </GridLayout>

代码解释:

代码很简单,就是清除和回退按钮设置了跨两列而已,其他的都是直接添加的

每个组件默认是占一行,占一列

这里要说明一点:

通过android:layout_rowSpan和android:layout_columnSpan设置表明组件横越的行数与列数

再通过:android:layout_gravity = "fill"  设置表明组件填满所横越的整行或者整列

用法总结:

①GridLayout使用虚细线将布局划分为行,列和单元格,同时也支持在行,列上进行交错排列

②使用流程:

step 1:先定义组件的对其方式 android:orientation  水平或者竖直

step 2:设置组件所在的行或者列,记得是从0开始算的

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

另外:这些属性也常用到:

<GridView android:id="@+id/grid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:verticalSpacing="35px" <!-- grid元素之间的竖直间隔 -->
    android:horizontalSpacing="5px" 
<!--grid元素之间的水平间隔  -->
    android:numColumns="auto_fit" <!--表示有多少列,如果设置为auto_fit,将根据columnWidth和Spacing来自动计算 -->
    android:columnWidth="100px" <!-- 一般建议采用有像素密度无关的dip或者dp来表示-->
    android:stretchMode="columnWidth"
<!--如何填满空余的位置,模拟器采用WVGA800*480,每排4列,有4*100+5*3=415,还余65px的空间,如果是columnWidth,则这剩余的65将分摊给4列,每列增加16/17px。如果采用SpacingWidth,则分摊给3个间隔空隙
-->
    android:gravity="center"  />

GridLayout(网格布局)的更多相关文章

  1. Android精通:TableLayout布局,GridLayout网格布局,FrameLayout帧布局,AbsoluteLayout绝对布局,RelativeLayout相对布局

    在Android中提供了几个常用布局: LinearLayout线性布局 RelativeLayout相对布局 FrameLayout帧布局 AbsoluteLayout绝对布局 TableLayou ...

  2. Android零基础入门第32节:新推出的GridLayout网格布局

    原文:Android零基础入门第32节:新推出的GridLayout网格布局 本期主要学习的是网格布局是Android 4.0新增的布局,和前面所学的TableLayout表格布局 有点类似,不过他有 ...

  3. Android之GridLayout网格布局

    1.相关属性 GridLayout网格布局是4.0之后引入的布局方式! android:columnCount="4" //设置列数(4列) android:rowCount=&q ...

  4. android的布局-----GridLayout(网格布局)

    学习导图 (一)简介 网格布局由GridLayout所代表,在android4.0之后新增加的布局管理器,因此需要android4.0之后的版本中使用,如果在更早的平台使用该布局管理器,则需要导入相应 ...

  5. GridLayout网格布局

    网格布局特点: l  使容器中的各组件呈M行×N列的网格状分布. l  网格每列宽度相同,等于容器的宽度除以网格的列数. l  网格每行高度相同,等于容器的高度除以网格的行数. l  各组件的排列方式 ...

  6. java 图形化小工具Abstract Window Toolit ;布局管理器FlowLayout流式布局;BorderLayout边界布局;GridLayout网格布局;CardLayou重叠卡片布局;BoxLayout方框布局;绝对定位

    1.FlowLayout流式布局管理器: FlowLayout布局管理器中,组件像水流一样向某方向流动(排列),遇到障碍(边界)就折回,重头开始排列 .在默认情况下,FlowLayout局管理器从左向 ...

  7. Android——gridLayout(网格布局)

    <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android=" ...

  8. 浅谈GridLayout(网格布局)

    Android 4.0 布局-->GridLayout 网格布局 以行列单元格的形式展示内部控件排列,可以实现类似计算机键盘效果 ,也可以实现可自动变行的标签群效果 使用GridLayout , ...

  9. Android中帧布局-FrameLayout和网格布局-GridLayout

    帧布局-FrameLayout 一.概念 帧布局中,容器为每个加入其中的空间创建一个空白的区域(成为一帧).每个空间占据一帧,这些帧会按gravity属性自动对齐. 帧布局的效果是将其中的所有空间叠加 ...

随机推荐

  1. C# SendMessage用法一二

    函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.而函数PostMessage不同,将一个消息寄送到一个线程的消息队列后立即返回.  函数 ...

  2. IIS并发

    https://www.cnblogs.com/xinaixia/p/5945678.html

  3. 【转载】在AspNetCore 中 使用Redis实现分布式缓存

    原文地址:https://www.cnblogs.com/szlblog/p/9045209.html AspNetCore 使用Redis实现分布式缓存 上一篇讲到了,Core的内置缓存:IMemo ...

  4. NSNumber数字

    前言 将基本数据类型包装成 OC 对象 1.NSNumber 与 基本数据类型 的相互转换 // 基本数据类型 转 NSNumber // 对象方法,将整形数据转换为 OC 对象 NSNumber * ...

  5. 树状数组 二维偏序【洛谷P3431】 [POI2005]AUT-The Bus

    P3431 [POI2005]AUT-The Bus Byte City 的街道形成了一个标准的棋盘网络 – 他们要么是北南走向要么就是西东走向. 北南走向的路口从 1 到 n编号, 西东走向的路从1 ...

  6. CF431D Random Task 二分+数位dp

    One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. ...

  7. Python-6-字典-函数dict,字典的基本操作及将字符串设置功能用于字典

    phonebook = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'} 字典由键及其相应的值组成,这种键-值对称为项. 键必须为独一无二,值不必如 ...

  8. ubuntu下中文乱码解决

    这个方法只对该用户有效. 方法二:修改/etc/environment,增加以下内容: LANGUAGE=”zh_CN:zh:en_US:en” LANG=zh_CN.GBK

  9. html td 限制 高度 和 宽度

    td 要设置成 display : block td 里面的span 自动换行.. <td style="max-width: 150px;overflow-y:scroll;disp ...

  10. C#是类型安全语言

    C#是一种类型安全语言:所有的表达式都解析成某个类型的一个实例,在编译器生成的代码中,只会执行对这个类型来说有效的操作. [优势] 许多错误能在编译时就检测到,确保代码在执行这段代码前是正确的: 生成 ...