Layout layouts GridLayout Grid layout

Source: Internet
Author: User

Original Address http://blog.csdn.net/jianghuiquan/article/details/8299973

GridLayout Grid Layout

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 following are examples of the XML layout and the Java code layout, respectively:

  

First, the layout of XML mode

1. Create a blank activity

  

3. Open the "res/layout/activity_main.xml" file and modify it to the following code.

  

(1) Section ①

<?xml version= "1.0" encoding= "Utf-8"; each XML document starts with the XML prologue, and the first line in the preceding code is the XML preamble, <?xml version= "1.0" >. This line of code means parsing according to the 1.0 version of the XML rule. encoding = "Utf-8" indicates that the XML file is in UTF-8 encoded format. The encoding format can also be GB2312.

(2) Section ②

<gridlayout ... Represents the use of the Grid layout manager.

(3) Section ③

Android:layout_width= "Match_parent" android:layout_height= "match_parent" means that the layout manager width and high charge will fill the entire screen width and height.

(4) Section ④

android:orientation= "Horizontal" means a horizontal layout with a vertical vertical.

(5) Section ⑤

The Grid layout manager takes 5 rows and 4 columns.

4, we put 16 buttons button to GridLayout.

5, find different.

  

Let's look at the different places.

(1) Section ①

The target 0 button occupies 2 cells, and the current 0 button occupies 1 cells.

[HTML]View Plaincopy
    1. <Button
    2. android:id="@+id/zero"
    3. android:layout_columnspan="2"//Column extension two columns
    4. android:layout_gravity="Fill"//button fill with two full panes
    5. android:text="0"/>

(2) Section ②

The target button is in row 4th, column 3rd, and the current button is in row 4th 2nd column.

Workaround: After the 0 button occupies 2 cells, the button will automatically go to this position.

(3) Section ③

The target + button is in row 4th, column 4th, and the row expands by 2 rows; The current button is in the 4th row 3rd column.

Workaround: Since the 0 button occupies 2 cells, the target + will automatically go to this position.

[HTML]View Plaincopy
    1. <Button
    2. android:id="@+id/plus"
    3. android:layout_rowspan="2"//Line expansion two lines
    4. android:layout_gravity="Fill"//button fill with two full panes
    5. android:text="+"/>

(4) Section ④

target = button on line 5th, Occupy 3 column position; current = button is in row 4th 4th column.

Workaround: After the position is expanded by 0, the current = button will automatically go to line 5th, and the column expands to the same 0 button.

[HTML]View Plaincopy
    1. <Button
    2. android:id="@+id/equal"
    3. android:layout_columnspan="3"//Column Expansion 3 columns
    4. android:layout_gravity="Fill"//button fill full 3 grid
    5. android:text="="/>

Full Source code:

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <GridLayout//Network layout manager
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:orientation="Horizontal"//Horizontal direction
  7. android:rowcount="5"//5 line
  8. android:columncount="4" >//4 column
[HTML]View Plaincopy
  1. 16 x Buttons
  2. <Button
  3. android:id="@+id/one"
  4. android:text="1"/>
  5. <Button
  6. android:id="@+id/two"
  7. android:text="2"/>
  8.  <Button
  9. android:id="@+id/three"
  10. android:text="3"/>
  11. <Button
  12. android:id="@+id/devide"
  13. android:text="/"/>
  14. <Button
  15. android:id="@+id/four"
  16. android:text="4"/>
  17. <Button
  18. android:id="@+id/five"
  19. android:text="5"/>
  20. <Button
  21. android:id="@+id/six"
  22. android:text="6"/>
  23. <Button
  24. android:id="@+id/multiply"
  25. android:text="x"/>
  26. <Button
  27. android:id="@+id/seven"
  28. android:text="7"/>
  29. <Button
  30. android:id="@+id/eight"
  31. android:text="8"/>
  32. <Button
  33. android:id="@+id/nine"
  34. android:text="9"/>
  35. <Button
  36. android:id="@+id/minus"
  37. android:text="-"/>
  38. <Button
  39. android:id="@+id/zero"
  40. android:layout_columnspan="2"
  41. android:layout_gravity="Fill"
  42. android:text="0"/>
  43. <Button
  44. android:id="@+id/point"
  45. android:text="." />
  46. <Button
  47. android:id="@+id/plus"
  48. android:layout_rowspan="2"
  49. android:layout_gravity="Fill"
  50. android:text="+"/>
  51. <Button
  52. android:id="@+id/equal"
  53. android:layout_columnspan="3"
  54. android:layout_gravity="Fill"
  55. android:text="="/>
[HTML]View Plaincopy
    1. </GridLayout>

6, the final display effect is as follows:

      

Ii. layout of Java code mode

Now that we've learned to use XML for linearlayout layouts, let's learn how to do the same with Java code.

Temporary.

Off Topic:

[HTML]View Plaincopy
    1. <strong>absolutelayout Absolute layout </strong>
    2. Absolutelayout the absolute layout as if the div specified the absolute property, specifying the position of the element with X, y coordinates!
    3. The layout is now obsolete, know it!

Layout layouts GridLayout Grid layout

Related Article

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.