On the GridLayout layout of android4.0 development

Source: Internet
Author: User

Author: Lee rang

This paper focuses on some basic contents of the new GridLayout grid layout since the android4.0 version number, and realizes a simple calculator layout framework based on this. With this article, you can learn about some of the new features of Android UI development, and can implement relevant applications.


Before the android4.0 version number, the false idea is to achieve the effect of grid layout, first of all to consider the use of the most common linearlayout layout, but this arrangement will produce such as 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 the use of table layout tabellayout, which will include the elements in the form of rows and columns to arrange, each behavior of a TableRow object, can be a view object, but also in the TableRow can continue to add other controls, Each child control is added as a column. However, using such a layout may cause problems where the control cannot occupy multiple rows or columns, and rendering speed is not guaranteed to be very good.


android4.0 the GridLayout layout of the above version number has overcome 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. In fact, GridLayout uses a similar API to LinearLayout, just to change the relevant tags, so it is very easy for developers to master GridLayout. The GridLayout layout strategy is simply divided into the following three sections:

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 arrange its own line. On the other hand, for child controls in a GridLayout layout, the default is to set their display by Wrap_content, which is only required to be 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" represents the beginning of the first column, which is similar to the assignment of a one-dimensional array in a programming language.


Finally, suppose you need to set a control to span multiple rows or columns, just 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.

Simple calculator code written using the GridLayout layout such as the following (note: Only android4.0 and above version numbers):

<?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 "/> & Lt  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    oid: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>

Finally the implementation of the interface such as the following see:



References: http://tech.it168.com/a2011/1122/1277/000001277274.shtml

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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.