android4.0版本后新增了一个GridLayout,它使用虚细线将布局划分为行、列和单元格,也支持一个控件在行、列上都有交错排列,其实用方法和LinearLayout,Relativelayout等类似,只不过多了一些特有的属性。

GridLayout的布局策略简单分为以下三个部分:

  首先它与LinearLayout布局一样,也分为水平和垂直两种方式,默认是水平布 局,一个控件挨着一个控件从左到右依次排列,但是通过指定android:columnCount设置列数的属性后,控件会自动换行进行排列。另一方面, 对于GridLayout布局中的子控件,默认按照wrap_content的方式设置其显示,这只需要在GridLayout布局中显式声明即可。

其次,若要指定某控件显示在固定的行或列,只需设置该子控件的android:layout_row和android:layout_column属性即 可,但是需要注意:android:layout_row=”0”表示从第一行开始,android:layout_column=”0”表示从第一列开 始,这与编程语言中一维数组的赋值情况类似。

最后,如果需要设置某控件跨越多行或多列,只需将该子控件的android:layout_rowSpan或者layout_columnSpan属性
设置为数值,再设置其layout_gravity属性为fill即可,前一个设置表明该控件跨越的行数或列数,后一个设置表明该控件填满所跨越的整行或
整列。

  可以说使用GridLayout以后可以完全不用tablelayout了,而且使用GridLayout有效减少了布局的深度,提高了app整体的性能质量。

  下面是使用GridLayout完成的效果图:

  

  布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/digital_bg"
android:columnCount="6"
android:orientation="horizontal"
android:padding="16dip"
android:rowCount="3">
<TextView
android:id="@+id/edit_input"
android:layout_columnSpan="5"
android:layout_gravity="fill"
android:textSize="14sp"
android:gravity="center_vertical"
android:paddingLeft="8dip"
android:background="@drawable/input_bg" /> <ImageButton
android:id="@+id/delete"
android:layout_marginLeft="16dip"
android:background="@drawable/delete_btn_style" />
    <ImageButton
android:id="@+id/num_1"
android:layout_marginTop="16dip"
android:background="@drawable/num_1_btn_style" /> <ImageButton
android:id="@+id/num_2"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_2_btn_style" /> <ImageButton
android:id="@+id/num_3"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_3_btn_style" /> <ImageButton
android:id="@+id/num_4"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_4_btn_style" /> <ImageButton
android:id="@+id/num_5"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_5_btn_style" />
   <ImageButton
android:id="@+id/confirm"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:layout_rowSpan="2"
android:background="@drawable/confirm_btn_style" /> <ImageButton
android:id="@+id/num_6"
android:layout_marginTop="16dip"
android:background="@drawable/num_6_btn_style" /> <ImageButton
android:id="@+id/num_7"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_7_btn_style" /> <ImageButton
android:id="@+id/num_8"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_8_btn_style" /> <ImageButton
android:id="@+id/num_9"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_9_btn_style" /> <ImageButton
android:id="@+id/num_0"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_0_btn_style" /> </GridLayout>

参考资料:http://blog.csdn.net/pku_android/article/details/7343258

android GridLayout布局的更多相关文章

  1. Android 4.0开发之GridLayOut布局实践

    在上一篇教程中http://blog.csdn.net/dawanganban/article/details/9952379,我们初步学习了解了GridLayout的布局基本知识,通过学习知道,Gr ...

  2. Android 优化布局层次结构

    前面介绍过使用HierarchyViewer和Android lint来优化我们的程序,这一篇算是总结性的,借助一个小例子来说用怎么优化应用布局.这个例子是android官网给出的,作者也当一把翻译. ...

  3. 浅谈android4.0开发之GridLayout布局

    作者:李响 本文重点讲述了自android4.0版本号后新增的GridLayout网格布局的一些基本内容,并在此基础上实现了一个简单的计算器布局框架.通过本文,您可以了解到一些android UI开发 ...

  4. Android 之布局

    1.RelativeLayout相对布局 a).第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_center ...

  5. 使用gridlayout布局后,因某些原因又删除,并整理文件夹结构时,Unable to resolve target &#39;android-7&#39;

    出现的问题 [2013-01-11 10:52:39 - gridlayout_v7] Unable to resolve target 'android-7' 事由:在一次做九宫格时.误使用了gri ...

  6. Android五大布局详解——LinearLayout(线性布局)

    Android五大布局 本篇开始介绍Android的五大布局的知识,一个丰富的界面显示总是要有众多的控件来组成的,那么怎样才能让这些控件能够按你的想法进行摆放,从而自定义你所想要的用户界面呢?这就牵涉 ...

  7. 【腾讯Bugly干货分享】Android动态布局入门及NinePatchChunk解密

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57c7ff5d53bbcffd68c64411 作者:黄进——QQ音乐团队 摆脱 ...

  8. Xamarin.Android之布局文件智能提示问题

    一.前言 看到有人问关于xamarin.android的布局没智能提示问题(VS 2015),当然,写布局这东西没提示这是一件相对痛苦的事 ,所以这里就提供一个解决的方案! 二.解决方案 想要智能提示 ...

  9. android—-线性布局

    android五大布局之线性布局. 1.线性布局的特点:各个子元素彼此连接,中间不留空白 而今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用 用的比较多的就是L ...

随机推荐

  1. webuploader php上传视频

    webuploader 上传大视频文件 在网上找了一个,自己重新组合了下,两个主要的文件,再加上官方下载的文件.其中有几个重要的点. 1.上传存放视频目录为了测试 直接777 2.fileupload ...

  2. maven项目诡异的问题

    install 的 war包中 dao 文件夹下没有xml文件 解决:重新注册为maven项目

  3. struts2 一些注解

    实现的JSP页面位置 web-root/jsp/user/add.jsp /update.jsp // /* @Namespace("/t") @AllowedMethods(va ...

  4. OpenGL常用的函数

    OpenGL常用的函数 1. void glBegin(GLenummode) void glEnd(void) 参数说明: mode:创建图元的类型.可以是以下数值 GL_POINTS:把每一个顶点 ...

  5. IDEA14添加SVN

    今天升级IDEA14,发现SVN变了,折腾了好一会才搞好.记录一下,希望其他人能少走弯路.主要是IDEA14的svn是用命令行工具进行操作,这样对以后SVN的兼容性也会好很多,这个在他们官方的博客上也 ...

  6. EBS Webservice Timeout,HTTP Server Return "500 Internal Server Error"

    http://blog.itpub.net/26687597/viewspace-1207571/ 基于Oracle EBS R12,开发了一个Webservice用于返回某项主数据,当请求的数据量非 ...

  7. 转 UNIGUI安装教程、使用例子

    转 UNIGUI安装教程.使用例子 http://my.oschina.net/u/582827/blog/203429?p={{currentPage-1}} 转 uniGui安装教程.使用例子 发 ...

  8. 序列化Json时遇到的大小写问题及解决方法

    最近在一个webapi2项目中遇到了一个问题:C#编码规范中规定属性的首字母是大写的(大多数公司采用这种编码风格),但是从其它系统中接收到的json对象的属性却是小写的(大多数公司采用这种编码风格), ...

  9. C#基础复习(1) 之 Struct与Class的区别

    参考资料 [1] 毛星云[<Effective C#>提炼总结] https://zhuanlan.zhihu.com/p/24553860 [2] <C# 捷径教程> [3] ...

  10. NetCore入门篇:(五)Net Core项目使用静态文件

    一.简介 1.Net Core默认无法访问静态文件,需要在Startup通过代码添加定义. 2.本文介绍两种静态文件目录实现方式. 二.启用默认目录 1.添加图片文件 2.测试访问结果(不能访问) 3 ...