继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

繁星点点滴滴
关注TA
已关注
手记 217
粉丝 67
获赞 333

   本期主要学习的是网格布局是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代码:

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

<?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零基础入门技术讨论微信群,共同成长!

原文链接:http://www.apkbus.com/blog-205190-68625.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP