GridLayout and gridlayout
Preface:
As a new layout after android 4.0, it is a little similar to the TableLayout (table layout) introduced earlier;
However, some new things are added.
① Like LinearLayout (linear layout), it can set the alignment of components in the container.
② Components in the container can span multiple rows or multiple columns (the component is directly placed in comparison to the TableLayout component and occupies one row)
Because it was added to android 4.0, API Level 14, the sdk earlier than this version
You need to import the project.
Common attributes: arrange and align:
① Set the component arrangement mode: android: orientation = "" vertical (vertical, default) or horizontal (horizontal)
② Set the alignment mode of the component: android: layout_gravity = "" center, left, right, buttom. If you want to use these two types at the same time: eg: buttom | left
Set the layout to several rows and columns:
① Set the number of rows: android: rowCount = "4" // set the grid layout to 4 rows
② Set the number of columns: android: columnCount = "4" // set the grid layout to 4 columns.
Set the number of rows and columns of a component.
Note: they all start from 0!
① When the component is located in the following row: android: layout_row = "1" // you can specify the second row as the component.
② Column number of the component: android: layout_column = "2" // you can specify the third column for the component.
Set the number of columns that a component spans in several rows:
① Span several lines: android: layout_rowSpan = "2" // vertically span two lines
② Span several columns: android: layout_columnSpan = "3" // horizontally span 2 columns
Example: The most common example-calculator interface:
:
PS: the grid layout is different from other Layout_width and Layout_height attributes.
The component width and height are determined by several rows and columns. Of course, you can also write a wrap_content
Code:
[Html] view plaincopy
Android: id = "@ + id/GridLayout1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: rowCount = "6"
Android: columnCount = "4"
Android: orientation = "horizontal">
Android: layout_columnSpan = "4"
Android: text = "0"
Android: textSize = "50sp"
Android: layout_marginLeft = "5dp"
Android: layout_marginRight = "5dp"
/>
Android: text = "rollback"
Android: layout_columnSpan = "2"
Android: layout_gravity = "fill"
/>
Android: text = "clear"
Android: layout_columnSpan = "2"
Android: layout_gravity = "fill"
/>
Android: text = "+"
/>
Android: text = "1"
/>
Android: text = "2"
/>
Android: text = "3"
/>
Android: text = "-"
/>
Android: text = "4"
/>
Android: text = "5"
/>
Android: text = "6"
/>
Android: text = "*"
/>
Android: text = "7"
/>
Android: text = "8"
/>
Android: text = "9"
/>
Android: text = "/"
/>
Android: layout_width = "wrap_content"
Android: text = "."
/>
Android: text = "0"
/>
Android: text = "="
/>
Code explanation:
The code is simple, that is, the clear and rollback buttons are set to cross-two columns. Others are directly added.
By default, each component occupies one row and one column.
One thing to note:
You can use the android: layout_rowSpan and android: layout_columnSpan settings to indicate the number of rows and columns that the component crosses.
Then, android: layout_gravity = "fill" indicates that the component fills up the whole row or whole column
Usage summary:
① GridLayout divides the layout into rows, columns, and cells using virtual fine lines. It also supports the staggered arrangement of rows and columns.
② Procedure:
Step 1: first define the method of the component android: orientation horizontal or vertical
Step 2: Set the row or column where the component is located. Remember to calculate it from 0.
Step 3: set the number of rows or columns that the component spans. After setting, you need to set a fill: android: layout_gravity = "fill"
Possible problems:
When you set the layout to GridLayout, an inexplicable error occurs,
If the code syntax logic is correct, the problem may be caused by the AndroidManifest. xml configuration file.
Because GridLayout was launched only after android 4.0, the API Level is 14
You only need to change the MinSDK in the configuration file to version 14 or later. Save the settings and solve the problem!
GridLayout for earlier sdk versions
In fact, you only need to export a package:
Download the v7 package
Import: GridLayout directory under the sdk: sdk \ extras \ android \ support \ v7 \ gridlayout
Then add the library for the project to be used.
Note that some beginners won't even pour the package. Here is a demonstration of the package guide process:
Find sdk \ extras \ android \ support \ v7 \ gridlayout and click OK.
, The library exists (ps: because the author's sdk is 4.2, it will display an exclamation point ..)
Add it to the project:
After selection:
It indicates that the file is successfully added, and then we can use it directly in the xml file.
In XML, you only need to write GridLayout as follows:
And add a row of namespace, instead of replacing:
Xmlns: app = "https://schemas.android.com/apk/res-auto"
You can.