Android Basics Getting Started tutorial--2.2.5 GridLayout (Grid layout)
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
The layout to be introduced today is a new layout introduced after Android 4.0, and the Tablelayout (table layout) you learned earlier
A bit similar, but he has a lot of things that the former does not, but also more useful,
- You can set the arrangement of components in a layout yourself
- You can customize how many rows the grid layout has, how many columns
- You can set the component directly in a row of a column
- You can set the component to span several rows or columns
In addition to the above, this section will also give you the use of GridLayout problems encountered, and how to resolve the low version
How the SDK uses the GridLayout Method! Let's start with the lessons in this section!
1. Related Properties Summary diagram
2. Use example: the implementation of the calculator layout:
Run:
Implementation code:
<gridlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns:tools =" Http://schemas.android.com/tools " android:id = "@+id/gridlayout1" android:layout_width =" wrap_content " android:layout_height =" wrap_content " android:columncount =" 4 " Android:orientation = "horizontal" android: RowCount = "6" ; <textview android:layout _columnspan = "4" android:layout_gravity = "fill" android:layout_marginleft =< Span class= "Hljs-value" > "5DP" android:layout_marginright =" 5DP " android:background =" # FFCCCC " android:text =" 0 " android:textsize = "50sp" /> <buttonandroid:layout_columnspan= "2"android:layout_gravity=" Fill "android:text=" fallback " /> <buttonandroid:layout_columnspan="2"android:layout_gravity= "Fill" Android:text="Empty" /> <button android:text="+" /> <button android:text="1" /> <button android:text="2" /> <button android:text="3" /> <button android:text="-" /> <button android:text="4" /> <button android:text="5" /> <button android:text="6" /> <button android:text="*" /> <button android:text="7" /> <button android:text="8" /> <button android:text="9" /> <button android:text="/" /> <buttonandroid:layout_width= "wrap_content"android:text=" ." /> <button android:text="0" /> <button android:text="=" /></GridLayout>
Code parsing:
The code is simple, just fallback with clear buttons across two columns, while others are added directly, by default each component is
In one row, there's another thing to note:
We pass:android:layout_rowspan and Android:layout_columnspan set the components across
If you want the component to fill a row or column that crosses across rows or columns, you need to add the following attribute:
android:layout_gravity = "Fill"!!! Just like this computer shows the number of parts!
3. Usage Induction:
①gridlayout use a dashed line to divide the layout into rows, columns, and cells, while also supporting rows and columns for staggered arrangement
② Use process:
- Step 1: Define how the component is android:orientation horizontally or vertically, and how many rows and columns to set
- Step 2: Set the row or column where the component is located, remember that it is calculated from 0, do not set the default per component one row
- Step 3: Set the component across several rows or columns; After Setup, you need to set a fill: android:layout_gravity = "Fill"
4. Places to be aware of when using GridLayout:
Because Girdlayout is released after 4.0, the MINSDK version should be changed to 14 or more,
Otherwise write the layout code, this thing will be inexplicably error, said to find this gridlayout,
Of course, if you want the lower version compatible, it is necessary to look at the following content!
5. How the low version SDK uses GridLayout:
The workaround is simple: just import the GridLayout package of the V7 package!
V7 package is generally under the SDK: Sdk\extras\android\support\v7\gridlayout directory
If you don't have one, you can also download it here:
Gridlayout_v7_jay Package Download: http://pan.baidu.com/s/1kTC2s3l
But when used, the label is written like this:!!!
<android.support.v7.widget.GridLayout>
Summary of this section
The introduction to GridLayout is here ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started tutorial--2.2.5 GridLayout (Grid layout)