Http://tech.it168.com/a2011/1122/1277/000001277274.shtml
This paper focuses on some basic content of the new GridLayout grid layout since the android4.0 version, and implements a simple calculator layout framework. With this article, you can learn about some of the new features of Android UI development and the ability to implement relevant applications.
Before the android4.0 version, if you want to achieve the effect of grid layout, you can first consider using the most common linearlayout layout, but this arrangement will produce the following questions:
1. You cannot align controls at the same time in the X, Y axis direction.
2. There will be performance problems when the multi-layer layout is nested.
3. Some tools that support free editing layouts are not supported stably.
Second, consider using the table layout tabellayout, which arranges the contained elements in rows and columns, each of which behaves as a TableRow object or a View object, and in TableRow you can continue to add other controls. Each child control is added as a column. However, using this layout may cause problems where the control cannot occupy more than one row or column, and rendering speed is not guaranteed to be good.
The GridLayout layout of android4.0 above resolves the above problems. The GridLayout layout divides the layout into rows, columns, and cells using a dashed thin line, and also supports a control that has a jagged arrangement on rows and columns. and GridLayout use is actually similar to the LinearLayout API, just modify the relevant label only, so for developers, Master GridLayout is still very easy things. The layout strategy of GridLayout is divided into the following three parts:
First it is the same as the LinearLayout layout, also divided into horizontal and vertical, the default is a horizontal layout, a control is arranged from left to right next to a control, but by specifying Android:columncount to set the properties of the number of columns, the control will be automatically wrapped to arrange. On the other hand, for child controls in a GridLayout layout, the default is to set its display by Wrap_content, which is only explicitly declared in the GridLayout layout.
Second, to specify that a control is displayed in a fixed row or column, you only need to set the Android:layout_row and Android:layout_column properties of the child control, but note that: android:layout_row= "0" Indicates that starting from the first row, android:layout_column= "0" means starting from the first column, which is similar to the assignment of a one-dimensional array in a programming language.
Finally, if you need to set a control to span multiple rows or columns, simply set the Android:layout_rowspan or Layout_columnspan property of the child control to a number, and then set its Layout_gravity property to fill. The previous setting indicates the number of rows or columns that the control spans, and the next setting indicates that the control fills the entire row or column that spans.
The simple calculator code written using the GridLayout layout is as follows (note: only for android4.0 and above):
[HTML]View Plaincopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="Horizontal"
- android:rowcount="5"
- android:columncount="4" >
- <Button
- android:id="@+id/one"
- android:text="1"/>
- <Button
- android:id="@+id/two"
- android:text="2"/>
- <Button
- android:id="@+id/three"
- android:text="3"/>
- <Button
- android:id="@+id/devide"
- android:text="/"/>
- <Button
- android:id="@+id/four"
- android:text="4"/>
- <Button
- android:id="@+id/five"
- android:text="5"/>
- <Button
- android:id="@+id/six"
- android:text="6"/>
- <Button
- android:id="@+id/multiply"
- android:text="x"/>
- <Button
- android:id="@+id/seven"
- android:text="7"/>
- <Button
- android:id="@+id/eight"
- android:text="8"/>
- <Button
- android:id="@+id/nine"
- android:text="9"/>
- <Button
- android:id="@+id/minus"
- android:text="-"/>
- <Button
- android:id="@+id/zero"
- android:layout_columnspan="2"
- android:layout_gravity="Fill"
- android:text="0"/>
- <Button
- android:id="@+id/point"
- android:text="." />
- <Button
- android:id="@+id/plus"
- android:layout_rowspan="2"
- android:layout_gravity="Fill"
- android:text="+"/>
- <Button
- android:id="@+id/equal"
- android:layout_columnspan="3"
- android:layout_gravity="Fill"
- android:text="="/>
- </GridLayout>
The final implementation of the interface is as follows:
Reference: http://tech.it168.com/a2011/1122/1277/000001277274.shtml
Http://hb.qq.com/a/20111214/000865.htm
On the GridLayout layout of android4.0 development