亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Android零基礎入門第32節:新推出的GridLayout網格布局

標簽:
Android

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

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消