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. spring mvc 静态资源版本控制

    spring bean 文件中增加 <bean class="cn.zno.smse.common.context.VersionServletContext">< ...

  2. (字典树)How many--hdu--2609

    http://acm.hdu.edu.cn/showproblem.php?pid=2609 How many Time Limit: 2000/1000 MS (Java/Others)    Me ...

  3. MFC中的Invalidate、OnDraw、OnPaint函数的作用

    MFC中的Invalidate.OnDraw.OnPaint函数的作用 CWnd::Invalidate voidInvalidate( BOOL bErase = TRUE ); 该函数的作用是使 ...

  4. Time&Patience

    “时间和耐心,是世间最强大的两个勇士.” “对未来越有信心,对今天越有耐心,坚持做正确的事.” 每日必做清单 每天六组俯卧撑    活在当下    接收脆弱    一万小时理论(罗马不是一天建成的,胖 ...

  5. Delphi for iOS开发指南(5):在iOS应用程序中使用Calendar组件来选择日期

    http://blog.csdn.net/delphiteacher/article/details/8923519 在FireMonkey iOS应用程序中的Calendar FireMonkey使 ...

  6. WinRT 中后台任务类的声明

    要实现后台任务,需要实现IBackgroundTask接口 public sealed class SimpleTask : IBackgroundTask { public void Run(IBa ...

  7. Feed back TFS 2017 RC upgrade status to product team in product group 2017.03.01

    作为微软的MVP,有一个我最喜欢的好处,就是可以与产品组(产品研发部门)有零距离接触,可以最先拿到即将发版的产品,并且和产品组沟通,对产品中出现的问题实时反馈. 看到TFS产品组吸收了自己的建议和反馈 ...

  8. [翻译]NUnit---Property and Random Attributes(十四)

    小记:由于工作琐碎,没得心情翻译而且也在看<CLR vis C#>,所以断更了差不多5个月,现在继续翻译,保证会翻译完成,不会虎头蛇尾. 另:NUnit已经更新到2.6.3版本,虽然正在开 ...

  9. [ASP.NET].NET逻辑分层架构总结

    一.基础知识准备: 1.层的原则: (1)每一层以接口方式供上层调用. (2)上层只能调用下层. (3)依赖分为松散交互和严格交互两种. 2.业务逻辑分类: (1)应用逻辑. (2)领域逻辑. 3.采 ...

  10. LINQ to SQL语句大全

    LINQ to SQL语句大全     LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判 ...