文章目录

  • 一、简介
  • 二、常用属性介绍
  • 三、平分问题
  • 四、小米计算器效果
  • 五、动态加载

一、简介


GridLayout是Android4.0引入的网格布局,使用它可以减少布局嵌套。也算是常用,但一直没仔细看过,今天研究一下

二、常用属性介绍


GridLayout 使用属性

属性 作用
android:columnCount 最大列数
android:rowCount 最大行数
android:orientation GridLayout中子元素的布局方向
android:alignmentMode alignBounds:对齐子视图边界 alignMargins :对齐子视距内容,默认值
android:columnOrderPreserved 使列边界显示的顺序和列索引的顺序相同,默认是true
android:rowOrderPreserved 使行边界显示的顺序和行索引的顺序相同,默认是true
android:useDefaultMargins 没有指定视图的布局参数时使用默认的边距,默认值是false

item属性

属性 作用
android:layout_column 指定该单元格在第几列显示
android:layout_row 指定该单元格在第几行显示
android:layout_columnSpan 指定该单元格占据的列数
android:layout_rowSpan 指定该单元格占据的行数
android:layout_gravity 指定该单元格在容器中的位置
android:layout_columnWeight (API21加入)列权重
android:layout_rowWeight (API21加入) 行权重
android:layout_gravity 作用
center 不改变元素的大小,仅居中
center_horizontal 不改变大小,水平居中
center_vertical 不改变大小,垂直居中
top 不改变大小,置于顶部
left 不改变大小,置于左边
bottom 不改变大小,置于底部
right 不改变大小,置于右边
start 不改变大小,根据系统语言,置于开始位置
end 不改变大小,置于结尾
fill 拉伸元素控件,填满其应该所占的格子
fill_vertical 仅垂直方向上拉伸填充
fill_horizontal 仅水平方向上拉伸填充
clip_vertical 垂直方向上裁剪元素,仅当元素大小超过格子的空间时
clip_horizontal 水平方向上裁剪元素,仅当元素大小超过格子的空间时

注意

使用layout_columnSpanlayout_rowSpan时要加上layout_gravity属性,否则没有效果;另外item在边缘时宽高计算会出现错误,需要我们手动设置宽高,否则达不到想要的效果

三、平分问题


GridLayout在API21时引入了android:layout_columnWeightandroid:layout_rowWeight来解决平分问题

那么在API21以前的,想要平分的话:引用兼容包

compile 'com.android.support:gridlayout-v7:25.+'

注意:

  1. 使用该控件,命名空间使用app
  2. 单独设置app:layout_columnWeight时,这一列的所有item都设置为这个属性,才能达到预期效果,否则这一列中设置了该属性的item,都会被隐藏,显示不出来
  3. 单独设置app:layout_rowWeight时,没有问题

四、小米计算器效果


<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/grid_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ece7e7"app:alignmentMode="alignBounds"app:columnCount="4"app:orientation="horizontal"app:rowCount="5"app:useDefaultMargins="false"tools:context="com.strivestay.gridlayoutdemo.MainActivity"><!-- 如果不使用 app:layout_gravity="fill",则实际下面这个textview的宽度只是wrap_content,实现不了想要的right|bottom效果;或者,用app:layout_columnWeight="1",效果等同,填充满--><TextViewandroid:gravity="right|bottom"android:text="0"app:layout_columnSpan="4"app:layout_rowWeight="3"app:layout_columnWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="AC"android:textColor="#f68904"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="退格"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="/"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="*"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="7"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="8"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="9"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="—"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="4"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="5"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="6"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="+"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="1"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="2"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="3"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#f68904"android:gravity="center"android:text="="android:textColor="#ffffff"app:layout_columnWeight="1"app:layout_rowSpan="2"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="%"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="0"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="."app:layout_columnWeight="1"app:layout_rowWeight="1"/></android.support.v7.widget.GridLayout>

效果: 4.4.4模拟器

五、动态加载


1.xml引用GridLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/grid_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ece7e7"app:orientation="horizontal"app:useDefaultMargins="false"app:alignmentMode="alignBounds"tools:context="com.strivestay.gridlayoutdemo.MainActivity"></android.support.v7.widget.GridLayout>

2.动态添加

package com.strivestay.gridlayoutdemo;import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayout;
import android.view.Gravity;
import android.widget.TextView;
/*** GridLayout示例* @author StriveStay* @date 2018/3/27*/
public class MainActivity extends AppCompatActivity {private String[] mStrings = {"0","AC","退格","/","*","7","8","9","—","4","5","6","+","1","2","3","=","%","0","."};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// xml布局
//        setContentView(R.layout.activity_main);// 动态添加setContentView(R.layout.activity_main2);GridLayout gridLayout = (GridLayout) findViewById(R.id.grid_layout);// 6行   4列gridLayout.setColumnCount(4);gridLayout.setRowCount(6);for (int i = 0; i < mStrings.length; i++) {TextView textView = new TextView(this);GridLayout.LayoutParams params = new GridLayout.LayoutParams();params.width =0;params.height =0;if(i == 0){// 设置行列下标, 所占行列  ,比重// 对应: layout_row  , layout_rowSpan , layout_rowWeight// 如下代表: item坐标(0,0), 占 1 行,比重为 3 ; 占 4 列,比重为 1params.rowSpec = GridLayout.spec(0,1,3f);params.columnSpec = GridLayout.spec(0,4,1f);textView.setGravity(Gravity.BOTTOM|Gravity.RIGHT);}else{// 设置行列下标,和比重params.rowSpec = GridLayout.spec((i+3)/4,1f);params.columnSpec = GridLayout.spec((i+3)%4,1f);// 背景textView.setBackgroundColor(Color.WHITE);// 字体颜色if("AC".equals(mStrings[i])){textView.setTextColor(Color.parseColor("#f68904"));}if("=".equals(mStrings[i])){textView.setBackgroundColor(Color.parseColor("#f68904"));textView.setTextColor(Color.WHITE);params.rowSpec = GridLayout.spec((i+3)/4,2,1f);}// 居中显示textView.setGravity(Gravity.CENTER);// 设置边距params.setMargins(2,2,2,2);}// 设置文字textView.setText(mStrings[i]);// 添加itemgridLayout.addView(textView,params);}}
}

效果和用xml中直接布局一样:

注意:
GridLayout.spec(); 这个方法是一个重点,需要好好看一下,而且由于它有几个重载方法,使用时也要注意。比如说下面两个方法:

 public static Spec spec(int start, int size)public static Spec spec(int start, float weight)

我刚开始就忽略了这点,本想用的是第二个带有weight的方法,但是传入参数时,没有加上f,就调用了第一个方法,搞了半天才发现

所以,如果调用的是第二个方法,一定要注意float参数的表示方法,加个f,如:GridLayout.spec(0,1f);

GridLayout 使用总结相关推荐

  1. android功能网格布局,Visual Studio 开发安卓之布局-网格布局(GridLayout)

    GridLayout 与 TableLayout 用法非常相似,不过它的"单元格"可以跨行,没有了 Row 子元素,代码更简洁,还有据说它渲染速度比 TableLayout 快. ...

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

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

  3. 华丽丽的GridLayout-使用案例

    最近在阅读一份开源代码时,发现有一个页面中并不是明显的网格样式,但layout里的根控件用的是GridLayout,才发现原来它是可以这样用的,原来不一定拘泥于页面布局是明显的网格形式,而是需要我们来 ...

  4. 4.0以后的新布局方式GridLayout

    <?xml version="1.0" encoding="utf-8"?> <GridLayoutxmlns:android="h ...

  5. vs2008页面布局GridLayout绝对定位的设置

    如同数据库2000到2005的有些变化,让很多人找不到以往熟悉的选项,偶就曾经为找存储过程页浪费N多时间,现在转到VS2005同样有些东西让我烦恼,比如调出代码行数,比如页面布置默认由原来的GridL ...

  6. 简单使用gridlayout

    html部分 <!DOCTYPE html> <html> <head> <title></title> <link rel=&quo ...

  7. java计算器布局设计_Java图形化界面设计——布局管理器之GridLayout(网格布局) 之计算器...

    代码如下: import java.awt.*; import javax.swing.*; public class GridFrame extends JFrame { // 定义字符串数组,为按 ...

  8. android布局layout,Android布局(FrameLayout、GridLayout)

    1.帧布局(FrameLayout) 帧布局中的每一个组件都代表一个画面,默认以屏幕左上角作为(0,0)坐标,按组件定义的先后顺序依次逐屏显示,后面出现的会覆盖前面的画面.用该布局可以实现动画效果.继 ...

  9. android程序日历layout,Android使用GridLayout绘制自定义日历控件

    效果图 思路:就是先设置Gridlayout的行列数,然后往里面放置一定数目的自定义日历按钮控件,最后实现日历逻辑就可以了. 步骤: 第一步:自定义日历控件(初步) 第二步:实现自定义单个日期按钮控件 ...

  10. Android——GridLayout

    转载自http://www.cnblogs.com/over140/archive/2011/12/08/2280224.html 欢迎大家转载 前言 本章内容android.widget.GridL ...

最新文章

  1. logstash tcp multihost output(多目标主机输出,保证TCP输出链路的稳定性)
  2. 居然有人撸了一个网易云音乐云村,高手在民间!
  3. 前端学习(2832):小程序事件绑定2
  4. 使用率激增 250%,这份报告再次将 Serverless 推向幕前
  5. 小a与星际探索---DP
  6. 文件错误关于hibernate中报Duplicate class/entity mapping org.model.User错的问题
  7. python大作业数独_python做一个数独小游戏
  8. 图像视频信息库改直播服务器,短视频直播系统,开发流程详细解析
  9. 使用jstack和TDA进行java线程dump分析
  10. 一例Ext4文件系统fsck后损坏的修复过程
  11. JSP教程第9讲笔记
  12. pymysql操作数据库
  13. onclick与addEventListener的区别
  14. altium designer怎么添加元件库
  15. unity中的UV是什么
  16. AD不显示封装3D模型
  17. 为啥E进制计算机的效率最高?
  18. 查看mysql的用户名和密码_怎么查看mysql的用户名和密码
  19. pdf转图片在线转换免费
  20. ISO9000、CMM(I)、6sigma与对象分析技术

热门文章

  1. 中文和日文翻译的在线翻译网站
  2. android模拟M卡,Android模拟SD卡实现方法解析
  3. Redisson--使用/教程/实例
  4. C#中File类中文件的读取写入
  5. 虚幻蓝图实现只狼的钩索系统(1)-多球体检测
  6. 小霸王游戏机手柄(一)——硬件破解
  7. 尤里的复仇Ⅰ 小芳!
  8. 咸鱼Maya笔记—材质
  9. (十二)datetime数据类型
  10. 基于改进First_order的表情驱动图片系统(源码&教程)