zoukankan      html  css  js  c++  java
  • android4.0新特性添加了GridLayout布局

    在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及以上的版本):

     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:columnCount="4"
     6     android:orientation="horizontal" 
     7     android:layout_gravity="center_horizontal|center_vertical"
     8     >
     9 
    10     <Button
    11         android:id="@+id/one"
    12         android:text="1" />
    13 
    14     <Button
    15         android:id="@+id/two"
    16         android:text="2" />
    17 
    18     <Button
    19         android:id="@+id/three"
    20         android:text="3" />
    21 
    22     <Button
    23         android:id="@+id/devide"
    24         android:text="/" />
    25 
    26     <Button
    27         android:id="@+id/four"
    28         android:text="4" />
    29 
    30     <Button
    31         android:id="@+id/five"
    32         android:text="5" />
    33 
    34     <Button
    35         android:id="@+id/six"
    36         android:text="6" />
    37 
    38     <Button
    39         android:id="@+id/multiply"
    40         android:text="x" />
    41 
    42     <Button
    43         android:id="@+id/seven"
    44         android:text="7" />
    45 
    46     <Button
    47         android:id="@+id/eight"
    48         android:text="8" />
    49 
    50     <Button
    51         android:id="@+id/nine"
    52         android:text="9" />
    53 
    54     <Button
    55         android:id="@+id/minus"
    56         android:text="-" />
    57 
    58     <Button
    59         android:id="@+id/zero"
    60         android:layout_columnSpan="2"
    61         android:layout_gravity="fill"
    62         android:text="0" />
    63 
    64     <Button
    65         android:id="@+id/point"
    66         android:text="." />
    67 
    68     <Button
    69         android:id="@+id/plus"
    70         android:layout_gravity="fill"
    71         android:layout_rowSpan="2"
    72         android:text="+" />
    73 
    74     <Button
    75         android:id="@+id/equal"
    76         android:layout_columnSpan="3"
    77         android:layout_gravity="fill"
    78         android:text="=" />
    79 
    80 </GridLayout>

  • 相关阅读:
    PAT A1094 The Largest Generation (25 分)——树的bfs遍历
    PAT A1055 The World's Richest (25 分)——排序
    PAT A1052 Linked List Sorting (25 分)——链表,排序
    PAT A1076 Forwards on Weibo (30 分)——图的bfs
    辅导员
    辅导员面试
    C程序设计
    Excel VBA 基本概念
    Excel函数
    导入excel表的数据到数据库ssh
  • 原文地址:https://www.cnblogs.com/androidez/p/2985279.html
Copyright © 2011-2022 走看看