On the GridLayout layout of android4.0 development

Source: Internet
Author: User

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
  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="x"/>
  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>

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

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.