淺談GridLayout(網格布局)


Android 4.0 布局-->GridLayout 網格布局

以行列單元格的形式展示內部控件排列,可以實現類似計算機鍵盤效果 ,也可以實現可自動變行的標簽群效果

使用GridLayout ,有效減少了布局的深度,渲染速度也是很快的

 

類似於LinearLayout 的使用,額外添加了一些特有的屬性

 

先來看下GridLayout的一些屬性介紹

1、

android:orientation="horizontal|vertical"

內部控件是水平排列的還是豎直排列的

   與LinearLayout使用方法一致

2、

android:columnCount="4"
android:rowCount="4"

 

內部控件 一行或者一列的顯示條目個數,即一行/列 最多顯示幾條,這里是4條,當內部子控件數目多於時,自動換行/列顯示 ,使用此功能方便了實現了自動換行標簽群的功能

 

3、子控件屬性

   

android:layout_row = “3”

Android:layout_column = “3

 

   用於設置該控件的位置  注意從0開始 ,類似於數組,這里即該控件位置 第四行第四  列 

 

4、子控件屬性

   android:layout_rowSpan = “2”|  android:layout_columnSpan = “2”

   android:layout_gravity="fill"

 設置某控件跨越多行或多列,前一個設置表明該控件跨越的行數或列數,后一個設置表明該控件填滿所跨越的整行或 整列。

 

---------------------------------------------------------------------------------------------------------------- 

 示例(別人的代碼 ,直接拿來用了):

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5     <GridLayout
 6         android:layout_width="wrap_content"
 7         android:layout_height="wrap_content"
 8         android:layout_gravity="center"
 9         android:columnCount="4"
10         android:rowCount="4"
11         android:orientation="horizontal" >
12 
13         <Button
14             android:layout_column="3"
15             android:text="/" />
16 
17         <Button android:text="1" />
18 
19         <Button android:text="2" />
20 
21         <Button android:text="3" />
22 
23         <Button android:text="*" />
24 
25         <Button android:text="4" />
26 
27         <Button android:text="5" />
28 
29         <Button android:text="6" />
30 
31         <Button android:text="-" />
32 
33         <Button android:text="7" />
34 
35         <Button android:text="8" />
36 
37         <Button android:text="9" />
38 
39         <Button
40             android:layout_gravity="fill"
41             android:layout_rowSpan="3"
42             android:text="+" />
43 
44         <Button
45             android:layout_columnSpan="2"
46             android:layout_gravity="fill"
47             android:text="0" />
48 
49         <Button android:text="00" />
50 
51         <Button
52             android:layout_columnSpan="3"
53             android:layout_gravity="fill"
54             android:text="=" />
55 
56     </GridLayout>
57 </LinearLayout>
xml

效果圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM