GridLayout(網格布局)


常用屬性:

排列對齊:

①設置組件的排列方式:  android:orientation=""     vertical(豎直,默認)或者horizontal(水平)

②設置組件的對齊方式:  android:layout_gravity=""  center,left,right,buttom啊,這些,如果想同時用兩種的話:eg: buttom|left


設置布局為幾行幾列:

①設置有多少行:android:rowCount="4"       //設置網格布局有4行

②設置有多少列:android:columnCount="4"    //設置網格布局有4列


設置某個組件位於幾行幾列

注:都是從0開始算的哦!


①組件在第幾行:android:layout_row = "1"   //設置組件位於第二行

②組件在第幾列:android:layout_column = "2"   //設置該組件位於第三列


設置某個組件橫跨幾行幾列:

①橫跨幾行:android:layout_rowSpan = "2"    //縱向橫跨2行

②橫跨幾列:android:layout_columnSpan = "3"     //橫向橫跨2列


使用實例:

最最最普遍的例子----計算器界面:

效果圖:



PS:這里要說一點,網格布局和其他布局不同,可以不為組件設置Layout_width和Layout_height屬性

因為組件的寬高由幾行幾列決定了,當然,你也可以寫個wrap_content


代碼:

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/GridLayout1"  
  4.     android:layout_width="wrap_content"  
  5.     android:layout_height="wrap_content"  
  6.     android:rowCount="6"  
  7.     android:columnCount="4"  
  8.     android:orientation="horizontal">  
  9.   
  10.     <TextView   
  11.         android:layout_columnSpan="4"  
  12.         android:text="0"  
  13.         android:textSize="50sp"  
  14.         android:layout_marginLeft="5dp"  
  15.         android:layout_marginRight="5dp"  
  16.     />  
  17.   
  18.     <Button   
  19.         android:text="回退"   
  20.         android:layout_columnSpan="2"  
  21.         android:layout_gravity="fill"     
  22.     />  
  23.      
  24.     <Button   
  25.         android:text="清空"  
  26.         android:layout_columnSpan="2"  
  27.         android:layout_gravity="fill"      
  28.     />  
  29.       
  30.     <Button   
  31.         android:text="+"      
  32.     />  
  33.      
  34.     <Button   
  35.         android:text="1"      
  36.     />  
  37.     <Button   
  38.         android:text="2"      
  39.     />  
  40.     <Button   
  41.         android:text="3"      
  42.     />  
  43.     <Button   
  44.         android:text="-"      
  45.     />  
  46.     <Button   
  47.         android:text="4"      
  48.     />  
  49.     <Button   
  50.         android:text="5"      
  51.     />  
  52.     <Button   
  53.         android:text="6"      
  54.     />  
  55.     <Button   
  56.         android:text="*"      
  57.     />  
  58.     <Button   
  59.         android:text="7"      
  60.     />  
  61.     <Button   
  62.         android:text="8"      
  63.     />  
  64.     <Button   
  65.         android:text="9"      
  66.     />  
  67.     <Button   
  68.         android:text="/"      
  69.     />  
  70.     <Button   
  71.         android:layout_width="wrap_content"  
  72.         android:text="."      
  73.     />  
  74.     <Button   
  75.         android:text="0"      
  76.     />  
  77.      <Button   
  78.         android:text="="      
  79.     />  
  80. </GridLayout>  

 

代碼解釋:

代碼很簡單,就是清除和回退按鈕設置了跨兩列而已,其他的都是直接添加的

每個組件默認是占一行,占一列

這里要說明一點:

通過android:layout_rowSpan和android:layout_columnSpan設置表明組件橫越的行數與列數

再通過:android:layout_gravity = "fill"  設置表明組件填滿所橫越的整行或者整列


用法總結:

①GridLayout使用虛細線將布局划分為行,列和單元格,同時也支持在行,列上進行交錯排列

②使用流程:

step 1:先定義組件的對其方式 android:orientation  水平或者豎直

step 2:設置組件所在的行或者列,記得是從0開始算的

step 3:設置組件橫跨幾行或者幾列;設置完畢后,需要在設置一個填充:android:layout_gravity = "fill"

另外:這些屬性也常用到:

<GridView android:id="@+id/grid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:verticalSpacing="35px" <!-- grid元素之間的豎直間隔 -->
    android:horizontalSpacing="5px"  <!--grid元素之間的水平間隔  -->
    android:numColumns="auto_fit" <!--表示有多少列,如果設置為auto_fit,將根據columnWidth和Spacing來自動計算 -->
    android:columnWidth="100px" <!-- 一般建議采用有像素密度無關的dip或者dp來表示-->
    android:stretchMode="columnWidth" <!--如何填滿空余的位置,模擬器采用WVGA800*480,每排4列,有4*100+5*3=415,還余65px的空間,如果是columnWidth,則這剩余的65將分攤給4列,每列增加16/17px。如果采用SpacingWidth,則分攤給3個間隔空隙 -->
    android:gravity="center"  /> 


免責聲明!

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



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