GridLayout与TableLayout布局

记录下GridLayout与TableLayout布局的一些知识点,这两个布局对比起来弄,感觉明显比较更能加深印象。

GridLayout的使用设置:

GridLayout 布局在 Level14才被支持,之前版本要使用的话,要按以下步骤设置:

 1. import -> Existing Android Code Into Workspace  
 2. 选择目录: 在sdk下的GridLayout目录   
sdk\extras\android\support\v7\gridlayout 
 3. 勾选"Copy projects into workspace" 
TableLayout 的一些不足:  
至于有了TableLayout又搞个GridLayout的原因,简单试了下TableLayout,我认为至少有下面两个原因 
1.不能同时向水平和垂直方向做控件的对齐 
TableLayout继承了LinearLayout,因此只能向一个方向做控件的对齐。 
2.不能跨行跨列 
  因为TableLayout,不明确指定包含多少行,多少列,而是通过向TableRow里面添加其他组件,每添加一个组件该表格就增加一列。 
如果向TableLayout里面添加组件,那么该组件就直接占用一行。所以这种方式造成控件不能跨行跨列。 
而GridLayout,则用columnCount设置列数后,增加的控件在超过列数后自动换行进行排列。 
简单汇总下TableLayout的特点:  
Shrinkable : 该列的宽度可以进行收缩,以使表格能够适应父容器的大小 
Stretchable : 该列可以进行拉伸,以填满表格中空闲的空间 
Collapsed : 该列将会被隐藏 
GridLayout的特点:  
android:layout_row :  固定显示在第几行。 
android:layout_column :  固定显示在第几列,前面几列没控件的话就空着。 
android:layout_rowSpan : 跨几行 
android:layout_columnSpan:  跨几列  

用GridLayout弄计算器的例子都烂大街了,自己写了个好玩的例子来展示下这两个布局:


代码如下:

<?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:layout_gravity="center" 
   	android:orientation="vertical"
    android:rowCount="5"
    android:columnCount="4"    
    android:background="#FFE4C4" 
   > 	
   <ImageView      
        android:layout_column="0"
        android:layout_gravity="left|top"
        android:layout_row="0"
        android:src="@drawable/emoji_436" />
   	
   <TextView                           
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="left|top"                        
          android:layout_row="0"
          android:layout_column="1"
          android:layout_columnSpan="2"
          android:background="@drawable/chatfrom_bg_app_normal" 
      android:text="我喜欢你......(表白进行中)"                         
      	 />  
        	 	
    <TextView                               
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="right|top"
           android:layout_row="1"
      	   android:layout_column="1"
      	   android:layout_columnSpan="2"
           android:background="@drawable/chatto_bg_focused"                          
   	   android:text="一公斤苹果等于多少克?"            
           />  
        	 	
 	  <ImageView      
        android:layout_column="3"
        android:layout_gravity="right|top"
        android:layout_row="1"
        android:src="@drawable/emoji_443" />
   	 
 	   <ImageView       
        android:layout_column="0"
        android:layout_gravity="left|top"
        android:layout_row="2"
        android:src="@drawable/emoji_436" />
 	    	   
 	    <TextView                          
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="left|top"                        
          android:layout_row="2"
          android:layout_column="1"
          android:layout_columnSpan="2"
          android:background="@drawable/chatfrom_bg_app_normal" 
      android:text="啊,1024..."                         
      	 />          	 	
    <TextView                            
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="right|top"
           android:layout_row="3"
      	   android:layout_column="1"
      	   android:layout_columnSpan="2"
           android:background="@drawable/chatto_bg_focused"                          
   	   android:text="嗯,你是一个好人.但..."            
           />  
            	   
 	   	<ImageView
        android:id="@+id/imageView1"
        android:layout_column="3"
        android:layout_gravity="right|top"
        android:layout_row="3"
        android:src="@drawable/emoji_443" />
 	   
        <TableLayout 
         android:id="@+id/tablelayout1"            
         android:layout_row="4"
         android:layout_column="0"
         android:layout_columnSpan="4"
                     
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:shrinkColumns="1" 
    android:stretchColumns="2">
  
    <!-- 在TableLayout中,在TableRow以外,直接添加的对象会独占一行 -->
    <Button android:text="上面GridLayout,下面是TableLayout"/>
    <TableRow>
      <TextView
        	android:id="@+id/imageView222"
       		android:background="@drawable/chatto_bg_voiceforward_focused"
         	android:text="好人卡1"   />
      
      <TextView
        	android:id="@+id/imageView222"
       		android:background="@drawable/chatto_bg_voiceforward_focused"
         	android:text="好人卡2"   />
       
      <TextView
        	android:id="@+id/imageView222"
       		android:background="@drawable/chatto_bg_voiceforward_focused"
         	android:text="好人卡3"   />
    </TableRow>
  </TableLayout>
      
</GridLayout>


作者:李响       

   

     本文重点讲述了自android4.0版本后新增的GridLayout网格布局的一些基本内容,并在此基础上实现了一个简单的计算器布局框架。通过本文,您可以了解到一些android UI开发的新特性,并能够实现相关应用。


       在android4.0版本之前,如果想要达到网格布局的效果,首先可以考虑使用最常见的LinearLayout布局,但是这样的排布会产生如下几点问题:


1、不能同时在X,Y轴方向上进行控件的对齐。

2、当多层布局嵌套时会有性能问题。

3、不能稳定地支持一些支持自由编辑布局的工具。


       其次考虑使用表格布局TabelLayout,这种方式会把包含的元素以行和列的形式进行排列,每行为一个TableRow对象,也可以是一个View对象,而在TableRow中还可以继续添加其他的控件,每添加一个子控件就成为一列。但是使用这种布局可能会出现不能将控件占据多个行或列的问题,而且渲染速度也不能得到很好的保证。


        android4.0以上版本出现的GridLayout布局解决了以上问题。GridLayout布局使用虚细线将布局划分为行、列和单元格,也支持一个控件在行、列上都有交错排列。而GridLayout使用的其实是跟LinearLayout类似的API,只不过是修改了一下相关的标签而已,所以对于开发者来说,掌握GridLayout还是很容易的事情。GridLayout的布局策略简单分为以下三个部分:

首先它与LinearLayout布局一样,也分为水平和垂直两种方式,默认是水平布局,一个控件挨着一个控件从左到右依次排列,但是通过指定android:columnCount设置列数的属性后,控件会自动换行进行排列。另一方面,对于GridLayout布局中的子控件,默认按照wrap_content的方式设置其显示,这只需要在GridLayout布局中显式声明即可。

 

      其次,若要指定某控件显示在固定的行或列,只需设置该子控件的android:layout_row和android:layout_column属性即可,但是需要注意:android:layout_row=”0”表示从第一行开始,android:layout_column=”0”表示从第一列开始,这与编程语言中一维数组的赋值情况类似。


       最后,如果需要设置某控件跨越多行或多列,只需将该子控件的android:layout_rowSpan或者layout_columnSpan属性设置为数值,再设置其layout_gravity属性为fill即可,前一个设置表明该控件跨越的行数或列数,后一个设置表明该控件填满所跨越的整行或整列。

利用GridLayout布局编写的简易计算器代码如下(注意:仅限于android4.0及以上的版本):

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="horizontal"  
  6.     android:rowCount="5"  
  7.     android:columnCount="4" >  
  8.   <Button  
  9.         android:id="@+id/one"  
  10.         android:text="1"/>  
  11.   <Button  
  12.         android:id="@+id/two"  
  13.         android:text="2"/>  
  14.    <Button  
  15.         android:id="@+id/three"  
  16.         android:text="3"/>  
  17.   <Button  
  18.         android:id="@+id/devide"  
  19.         android:text="/"/>  
  20.   <Button  
  21.         android:id="@+id/four"  
  22.         android:text="4"/>  
  23.   <Button  
  24.         android:id="@+id/five"  
  25.         android:text="5"/>  
  26.   <Button  
  27.         android:id="@+id/six"  
  28.         android:text="6"/>  
  29.   <Button  
  30.         android:id="@+id/multiply"  
  31.         android:text="×"/>  
  32.   <Button  
  33.         android:id="@+id/seven"  
  34.         android:text="7"/>  
  35.   <Button  
  36.         android:id="@+id/eight"  
  37.         android:text="8"/>  
  38.   <Button  
  39.         android:id="@+id/nine"  
  40.         android:text="9"/>  
  41.     <Button  
  42.         android:id="@+id/minus"  
  43.         android:text="-"/>  
  44.     <Button  
  45.         android:id="@+id/zero"  
  46.         android:layout_columnSpan="2"  
  47.         android:layout_gravity="fill"  
  48.         android:text="0"/>  
  49.   <Button  
  50.         android:id="@+id/point"  
  51.         android:text="."/>  
  52.     <Button  
  53.         android:id="@+id/plus"  
  54.         android:layout_rowSpan="2"  
  55.         android:layout_gravity="fill"  
  56.         android:text="+"/>  
  57.     <Button  
  58.         android:id="@+id/equal"  
  59.         android:layout_columnSpan="3"  
  60.         android:layout_gravity="fill"  
  61.         android:text="="/>   
  62.   </GridLayout>  

最终实现的界面如下所示:



参考资料:http://tech.it168.com/a2011/1122/1277/000001277274.shtml

http://hb.qq.com/a/20111214/000865.htm



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值