Android——Android 4.0开发之GridLayOut布局实践

本文转自http://tech.it168.com/a2011/1213/1287/000001287977.shtml

  在最新的Android 4.0 SDK中,新引入了GridLayout的布局样式,这个布局样式看上去可能有点象之前的TableLayout,但实际上还是有所不同的。

  在上一篇教程中(http://tech.it168.com/a2011/1122/1277/000001277274.shtml),我们初步学习了解了GridLayout的布局基本知识,通过学习知道,GridLayout可以用来做一个象TableLayout这样的布局样式,但其性能及功能都要比tablelayout要好,比如GridLayout的布局中的单元格可以跨越多行,而tablelayout则不行,此外,其渲染速度也比tablelayout要快。在本文中,将指导读者进一步加深对GridLayout的认识,带大家实做一个简单的数字键盘布局,从中体会GridLayout的用法。

  开始设计

  首先,我们先设计下将要设计的键盘布局图,如下图:

可以看到这个布局的一些特点:

  1) 有5行4列

  2)每行的单元格和列方向的单元格的大小都是不一定相等的,比如“+”号这个按钮,在纵向上是横跨了两行的

  可以看到,如果要用传统的tablelayout布局样式,要实现以上的布局,可能要外加嵌套linarlayout布局样式,这样就会使的布局设计十分麻烦,而如果有了GridLayout样式,则可以很容易地解决这些问题。

GridLayout布局策略

  GridLayout布局样式和LinearLayout样式一样,可以有水平和垂直两个方向的布局方式。即如果设置为垂直方向布局,则下一个单元格将会在下一行的同一位置或靠右一点的位置出现,而水平方向的布局,则意味着下一个单元格将会在当前单元格的右边出现,也有可能会跨越下一行(因为有可能GridLayout布局样式中。在我们的这个例子中,如果从最右边的除号算起,使用水平布局的话则是4列,其代码如下所示:

1  <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
2     android:layout_width="wrap_content"
3     android:layout_height="wrap_content"
4     android:layout_gravity="center"
5     android:columnCount="4"
6     android:orientation="horizontal" > 
7 </GridLayout>

定义简单的单元格

  在GridLayout中,定义每个子控件跟以前使用布局中定义的方法有点不同,默认的是对所有的子控件使用wrap_content的方式,而不是显式声明宽度和高度并使用wrap_conent和match_parent,更多的相关规则可以参考GridLayout的文档,这里只需要在GridLayout本身的属性中,定义android:layout_width 均为wrap_conent即可。

  因此,我们接着在控件中,添加各个数字按钮,如下:

    <Button android:text="1" /> 
<Button android:text="2" />  
 
………………………

  运行后,可以看到一个初步的效果如下图所示:

定义特殊符号的位置

  可以看到,跟草稿的图相比,象除号,等于号等,位置不是很吻合,下面我们作些相应的调整,如下:

  1) 除号的大小可以不变,但它应该被放置在第4列出现

  2) +号应该放在数字9之后,并且它的高度要占3行之多

  3) 数字0应该占据两列的宽度

  4) 等于号应该占据三列

  为此,修改代码如下:

 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:layout_gravity="center"
 6     android:columnCount="4"
 7     android:orientation="horizontal" > 
 8   
 9     <Button 
10         android:layout_column="3"
11         android:text="/" /> 
12   
13     <Button android:text="1" /> 
14   
15   
16     <Button android:text="9" /> 
17   
18     <Button 
19         android:layout_rowSpan="3"
20         android:text="+" /> 
21   
22     <Button 
23         android:layout_columnSpan="2"
24         android:text="0" /> 
25   
26     <Button android:text="00" /> 
27   
28     <Button 
29         android:layout_columnSpan="3"
30         android:text="=" /> 
31   
32 </GridLayout>

在上面的代码中,可以看到,数字键3中,通过使用android:layout_column="3"指定数字从第4列开始(注意列的序号从0开始),而+号是紧跟在数字键9后,并且用android:layout_rowSpan="3"指出位于第4行,符号等于,则是紧跟着在数字“00”后,由于其layout_columnSpan=3,可以看到,占据了3个列的位置,因此另外重新新起一行进行了布局。运行后的结果如下图所示:

美化页面布局

  我们可以看到在上图中,依然出现了很多空位,跟我们预想的草稿图有一定差距,这里其实可以调整每个数字按钮中的位置即可,可以利用android 4.0 GridLayout布局中的

  layout_gravity属性,设置每个按钮中的位置,只需要设置layout_gravity属性为fill,即可将每个控件填充到其layout_columnSpan及layout_rowSpan所指定的宽度,修改后的代码如下所示:

 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:layout_gravity="center"
 6     android:columnCount="4"
 7     android:orientation="horizontal" > 
 8   
 9     <Button
10         android:layout_column="3"
11         android:text="/" /> 
12   
13     <Button android:text="1" /> 
14   
15     <Button android:text="2" /> 
16   
17     <Button android:text="3" /> 
18   
19     <Button android:text="*" /> 
20   
21     <Button android:text="4" /> 
22   
23     <Button android:text="5" /> 
24   
25     <Button android:text="6" /> 
26   
27     <Button android:text="-" /> 
28   
29     <Button android:text="7" /> 
30   
31     <Button android:text="8" /> 
32   
33     <Button android:text="9" /> 
34   
35     <Button
36         android:layout_gravity="fill"
37         android:layout_rowSpan="3"
38         android:text="+" /> 
39   
40     <Button
41         android:layout_columnSpan="2"
42         android:layout_gravity="fill"
43         android:text="0" /> 
44   
45     <Button android:text="00" /> 
46   
47     <Button
48         android:layout_columnSpan="3"
49         android:layout_gravity="fill"
50         android:text="=" /> 
51   
52 </GridLayout>

运行后,结果如下图:

posted @ 2013-07-24 17:27  艺言弈行  阅读(362)  评论(0编辑  收藏  举报