网格布局是Android4.0新增的布局管理器,因此需要在Android4.0之后的版本才可以使用,之前的平台使用该布局的话,需要导入相应的支持库。

GridLayout的作用类似于HTML中的table标签,它把整个容器划分成row*column个网格,每个网格都可以放置一个组件,也可以设置一个组件横跨多少列、多少行。

GridLayout提供了setRowCount(int)和setColumnCount(int)方法来控制该网格的行数和列数。

简单代码示例:

gridlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="6"
android:columnCount="4"
android:id="@+id/root"> <!-- 定一个一个横跨四列的文本框,并设置该文本框的前景色、背景色等属性 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:textSize="50sp"
android:layout_marginLeft="4px"
android:layout_marginRight="4px"
android:padding="5px"
android:layout_gravity="right"
android:background="#eee"
android:textColor="#000"
android:text="0"
/>
<!-- 定义一个横跨四列的按钮 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:text="清除"/> <!-- 添加其他按钮 -->
<Button
android:text="7"
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="8"
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="9"
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="/"
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="4"
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="5"
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="6"
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="*"
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="3"
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="-"
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="0"
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="."
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="+"
android:layout_rowWeight="1"
android:layout_columnWeight="1"/>
<Button
android:text="="
android:layout_rowWeight="1"
android:layout_columnWeight="1"/> </GridLayout>

  运行结果:

Android笔记(十一) Android中的布局——网格布局的更多相关文章

  1. Android 笔记之 Android 系统架构

    Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...

  2. Python pyQt4/PyQt5 学习笔记3(绝对对位,盒布局,网格布局)

    本节研究布局管理的内容. (一)绝对对位  import sys from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__( ...

  3. Android进阶(十一)Android系统架构讲解

    如果说一个成功男人的背后会有一个默默支持的女人的话,那么一个优越稳定的平台背后必有一个成熟的系统架构所支撑着.那么,Android的系统架构是怎么样的呢?从下图我们可以从整体上有个大致的了解(图片来源 ...

  4. Android 基础 十一 Android的消息机制

    Handler是Android消息机制的上层接口,这使得在开发应用过程中我们只需要和Handler交互即可.Handler的使用过程很简单,通过它可以轻松地将一个任务切换到Handler所在的线程中去 ...

  5. Android笔记: Android版本号

    由于有2套版本号 总是对应不准 记下来做过标记 Android 4.3 ----18 Android 4.2---17 Android 4.1---16 Android 4.0.3---15Andro ...

  6. Android笔记:java 中的数组

    在与嵌入式设备通讯的过程中使用的socket通讯 获取的字节流,通常转换为字节数组,需要根据协议将字节数组拆分.对于有规律的重复拆分可以使用,由于java中不能像c中直接进行内存操作例如使用struc ...

  7. Android笔记:java 中的枚举

    部分数据使用枚举比较方便,java中的enmu不如c#中使用方便 记录备忘 以c#中的代码为例 public enum PlayState { /// <summary> /// 关闭 / ...

  8. Android笔记之Fragment中创建ViewModel的正确方式

    之前一直都是这么写的 pageViewModel = ViewModelProviders.of(this).get(PageViewModel.class); //参数this是当前fragment ...

  9. Java学习笔记十一:Java中的方法

    Java中的方法 一:什么是方法: 所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块. 学过C语言或者其他语言的应该都知道函数这个东西,在Java中,其实方法就是函数,只不过叫法不同,在 ...

随机推荐

  1. gitlab 配置到jenkins

    直接把公匙配到了gitlab,然后填入jenkins https://blog.csdn.net/zhufengyan521521/article/details/81219193  配置全局凭据 h ...

  2. antd 用 customize-cra 方式引入 sass

    antd 用 customize-cra 方式引入 sass 只需要安装:node-sass 即可

  3. 【k8s node断电重启】

    kubernetes断电重启 导致部分pod无法删除 dashboard上处于黄色 kubectl get处于terminate 状态 kubectl delete报错: An error occur ...

  4. SPSS 习题-2

    1.有关SPSS数据字典的说法,正确的是:  AA. SPSS 数据集的数据字典可以复制到其他数据集中B. SPSS数据集的数据字典是不能复制的C. SPSS的数据字典可以通过“复制”和“粘贴”在不同 ...

  5. 【CUDA开发】CUDA面内存拷贝用法总结

    [CUDA开发]CUDA面内存拷贝用法总结 标签(空格分隔): [CUDA开发] 主要是在调试CUDA硬解码并用D3D9或者D3D11显示的时候遇到了一些代码,如下所示: CUdeviceptr g_ ...

  6. 对偶传播神经网络(CPN)

    1987年,美国学者Robert Hecht-Nielsen提出了对偶传播神经网络模型 (Counter Propagation Network,CPN),CPN最早是用来实现样本选择匹配系统的.CP ...

  7. 深入css过渡transition

    通过过渡transition,可以让web前端开发人员不需要javascript就可以实现简单的动画交互效果.过渡属性看似简单,但实际上它有很多需要注意的细节和容易混淆的地方. 过渡transitio ...

  8. Python17之函数、类、模块、包、库

    一.函数 一个拥有名称.参数和返回值的代码块. 需要主动调用,否则不会执行,可以通过参数和返回值与其它程序进行交互 二.类 用来描述具有相同的属性和方法的对象集合.它定义了该集合中每个对象所共有的属性 ...

  9. C++ 中 static 与 const 的用法及对比

    在这个学习过程中我对 static 及 const 的使用时常会混淆,因此整理,加深记忆 一.类的静态成员 如果某个属性为整个类所共有,不属于任何一个具体对象,则采用 static 关键字来声明静态成 ...

  10. 09 IO流(六)——ByteArray字节流、流对接

    字节数组流 ByteArray流是节点流. 前面讲到的文件字节流,文件字符流,他们的源都是来自于pc硬盘. 本节要讲的字节数组流的源是来自于内存或网络. 它更合适用来处理来自网络的字节流. 由于源并不 ...