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

本期主要学习的是网格布局是Android 4.0新增的布局,和前面所学的TableLayout表格布局 有点类似,不过他有很多前者没有的东西,也更加好用。

一、认识GridLayout

网格布局实现了控件的交错显示,能够避免因布局嵌套对设备性能的影响,更利于自由布局的开发。网格布局用一组无限细的直线将绘图区域分成行、列和单元,并指定控件的显示区域和控件在该区域的显示方式

下表显示了 GridLayout常用的XML属性及相关方法说明。

为了控制GridLayout布局容器中各子组件的布局分布,GridLayout提供了一个内部类: GridLayout.LayoutParams,该类提供了大量的XML属性来控制GridLayout布局容器中子组件的布局分布。

下表显示了 GridLayout.LayoutParams常用的XML属性及相关方法。

二、示例

接下来通过一个简单的示例程序来学习GridLayout的使用用法。

同样使用WidgetSample工程,继续使用app/main/res/layout/目录下的activity_main.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:columnCount="4"
android:rowCount="7"> <TextView
android:id="@+id/result_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:background="#eee"
android:text="0"
android:textColor="#000"
android:textSize="50sp" /> <Button
android:id="@+id/clear_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:text="Clear" />
<Button
android:id="@+id/one_btn"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="1" />
<Button
android:id="@+id/two_btn"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="2" />
<Button
android:id="@+id/three_btn"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="3" />
<Button
android:id="@+id/devide_btn"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="/" />
<Button
android:id="@+id/four_btn"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="4" />
<Button
android:id="@+id/five_btn"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="5" />
<Button
android:id="@+id/six_btn"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="6" />
<Button
android:id="@+id/multiply_btn"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="×" />
<Button
android:id="@+id/seven_btn"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="7" />
<Button
android:id="@+id/eight_btn"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="8" />
<Button
android:id="@+id/nine_btn"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="9" />
<Button
android:id="@+id/minus_btn"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="-" />
<Button
android:id="@+id/zero_btn"
android:layout_columnSpan="2"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:text="0" />
<Button
android:id="@+id/point_btn"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="." />
<Button
android:id="@+id/plus_btn"
android:layout_columnWeight="1"
android:layout_rowSpan="2"
android:layout_rowWeight="1"
android:text="+" />
<Button
android:id="@+id/equal_btn"
android:layout_columnSpan="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="=" />
</GridLayout>

运行程序,可以看到下图所示界面效果:

到此,关于Android中的六种界面布局已经学习完毕,你都掌握了吗?一定要多动手练习哦,从下期开始一起来学习Android事件处理。

今天就先到这里,如果有问题欢迎留言一起探讨,也欢迎加入Android零基础入门技术讨论微信群,共同成长!

此文章版权为微信公众号分享达人秀(ShareExpert)——鑫鱻所有,若需转载请联系作者授权,特此声明!

往期总结分享:

Android零基础入门第1节:Android的前世今生

Android零基础入门第2节:Android 系统架构和应用组件那些事

Android零基础入门第3节:带你一起来聊一聊Android开发环境

Android零基础入门第4节:正确安装和配置JDK, 高富帅养成第一招

Android零基础入门第5节:善用ADT Bundle, 轻松邂逅女神

Android零基础入门第6节:配置优化SDK Manager, 正式约会女神

Android零基础入门第7节:搞定Android模拟器,开启甜蜜之旅

Android零基础入门第8节:HelloWorld,我的第一趟旅程出发点

Android零基础入门第9节:Android应用实战,不懂代码也可以开发

Android零基础入门第10节:开发IDE大升级,终于迎来了Android Studio

Android零基础入门第11节:简单几步带你飞,运行Android Studio工程

Android零基础入门第12节:熟悉Android Studio界面,开始装逼卖萌

Android零基础入门第13节:Android Studio配置优化,打造开发利器

Android零基础入门第14节:使用高速Genymotion,跨入火箭时代

Android零基础入门第15节:掌握Android Studio项目结构,扬帆起航

Android零基础入门第16节:Android用户界面开发概述

Android零基础入门第17节:TextView属性和方法大全

Android零基础入门第18节:EditText的属性和使用方法

Android零基础入门第19节:Button使用详解

Android零基础入门第20节:CheckBox和RadioButton使用大全

Android零基础入门第21节:ToggleButton和Switch使用大全

Android零基础入门第22节:ImageView的属性和方法大全

Android零基础入门第23节:ImageButton和ZoomButton使用大全

Android零基础入门第24节:自定义View简单使用,打造属于你的控件

Android零基础入门第25节:简单且最常用的LinearLayout线性布局

Android零基础入门第26节:两种对齐方式,layout_gravity和gravity大不同

Android零基础入门第27节:正确使用padding和margin

Android零基础入门第28节:轻松掌握RelativeLayout相对布局

Android零基础入门第29节:善用TableLayout表格布局

Android零基础入门第30节:两分钟掌握FrameLayout帧布局

Android零基础入门第31节:少用的AbsoluteLayout绝对布局

Android零基础入门第32节:新推出的GridLayout网格布局的更多相关文章

  1. Android零基础入门第28节:轻松掌握RelativeLayout相对布局

    原文:Android零基础入门第28节:轻松掌握RelativeLayout相对布局 在前面三期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局. ...

  2. Android零基础入门第58节:数值选择器NumberPicker

    原文:Android零基础入门第58节:数值选择器NumberPicker 上一期学习了日期选择器DatePicker和时间选择器TimePicker,是不是感觉非常简单,本期继续来学习数值选择器Nu ...

  3. Android零基础入门第59节:AnalogClock、DigitalClock和TextClock时钟组件

    原文:Android零基础入门第59节:AnalogClock.DigitalClock和TextClock时钟组件 在前面一期,我们学习了DatePicker和TimePicker,在实际开发中其不 ...

  4. Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker

    原文:Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker 在实际开发中,经常会遇见一些时间选择器.日期选择器.数字选择器等需求,那么从本期开始来学习And ...

  5. Android零基础入门第56节:翻转视图ViewFlipper打造引导页和轮播图

    原文:Android零基础入门第56节:翻转视图ViewFlipper打造引导页和轮播图 前面两期学习了 ViewAnimator及其子类ViewSwitcher的使用,以及ViewSwitcher的 ...

  6. Android零基础入门第55节:ImageSwitcher和TextSwitcher使用

    原文:Android零基础入门第55节:ImageSwitcher和TextSwitcher使用 上一期我们了解了ViewAnimator组件和ViewSwitcher组件的使用,你都掌握了吗?本期一 ...

  7. Android零基础入门第54节:视图切换组件ViewSwitcher

    原文:Android零基础入门第54节:视图切换组件ViewSwitcher 前面三期学习了ProgressBar系列组件,那本期开始一起来学习ViewAnimator组件. 一.ViewAnimat ...

  8. Android零基础入门第53节:拖动条SeekBar和星级评分条RatingBar

    原文:Android零基础入门第53节:拖动条SeekBar和星级评分条RatingBar 前面两期都在学习ProgressBar的使用,关于自定义ProgressBar的内容后期会继续学习的,本期先 ...

  9. Android零基础入门第52节:自定义酷炫进度条

    原文:Android零基础入门第52节:自定义酷炫进度条 Android系统默认的ProgressBar往往都不能满足实际开发需要,一般都会开发者自定义ProgressBar. 在Android开发中 ...

随机推荐

  1. wpf datagrid 的单元格内容超出列宽度

    ---恢复内容开始--- <Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.mi ...

  2. python 多线程拷贝单个文件

    # -*- coding: utf-8 -*- # @author: Tele # @Time : 2019/04/04 下午 12:25 # 多线程方式拷贝单个文件 import threading ...

  3. python2.x脚本转换为python3.x脚本的方法

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/dushu990/article/details/73549174 python2.x脚本转换为pyt ...

  4. flex新的心得

    主要是对于flex: 与width: 的心得,先看代码. <head> <meta charset="UTF-8"> <meta name=" ...

  5. css3中的制作动画小总结

    系列教程 CSS3属性中有关于制作动画的三个属性:Transform,Transition,Animation: Transform 在CSS3中transform主要包括以下几种:旋转rotate. ...

  6. 面试问题:Vista与XP的Session 0与Session X的区别

    面试问题:Vista与XP的Session 0与Session X的区别 在XXXXX的一次面试中,笔试问题的题目曾提到Session 0.Session 1在Vista和Xp中的区别?现在把答案发上 ...

  7. Logical partitioning and virtualization in a heterogeneous architecture

    A method, apparatus, and computer usable program code for logical partitioning and virtualization in ...

  8. framework7使用问题汇总

    framework7 是个非常漂亮的Html框架,最近有个微信公众号的项目使用到了这个,后期还可以封装成APP. 淘宝版和中文官网都是V1,V2只能看英文版的http://framework7.io/ ...

  9. 参数方法(parameter)与非参数方法(nonparameter)

    参数方法表示参数固定,不随数据点的变化而变化: 非参数方法并不意味着没有参数,而是说,参数的数目随数据点而变化, 1. 参数方法举例 logistic regression:p(y=1|x,α)=11 ...

  10. MySQL复制slave服务器死锁案例

    原文:MySQL复制slave服务器死锁案例 MySQL复制刚刚触发了一个bug,该bug的触发条件是slave上Xtrabackup备份的时候执行flushs tables with read lo ...