GridLayout (grid layout)
Introduction to this section
The layout I will introduce today is a new layout introduced after Android 4.0, and the TableLayout (table layout) learned earlier It is somewhat similar, but it has many things that the former does not have, and is easier to use.
You can set the arrangement of components in the layout by yourself
You can customize how many rows and columns the grid layout has
You can directly set the component to be located in a certain row and column
You can set it The component spans several rows or columns
In addition, in addition to the above, this section will also tell you about the problems you will encounter when using gridLayout, and how to solve low-level versions How to use GridLayout method in sdk! Let’s start this lesson!
Related attribute summary diagram
2. Usage example: calculation Implementation of server layout:
Running rendering:
##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="5dp"
android:layout_marginRight="5dp"
android:background="../style/images/android-tutorial-gridlayout.html"
android:text="0"
android:textSize="50sp" />
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:text="回退" />
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:text="清空" />
<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" />
# & LT; Button android: Text = "/"/"/& GT;
## & LTON; Button
android: Layout_width =" wrap_content "#ANDROID: Text ="////// >
## ;
Code analysis:
The code is very simple, only the back and clear buttons span two columns, while the others are added directly. By default, each component is Occupying one row and one column, there is another thing to note: We set the component span through:
android:layout_rowSpan
and
android:layout_gravity = "fill"! ! ! Just like this part of the computer that displays numbers!3. Usage summary:①GridLayout uses dotted thin lines to divide the layout into rows, columns and cells, and also supports rows , staggered arrangement on the columns ②Usage process:
#step 1: First define the orientation of the components android:orientation horizontal or vertical, set how many rows and columns
step 2: Set the row or column where the component is located. Remember to count from 0. If not set, each component will occupy one row and one column by default5. How to use GridLayout with lower version sdk:
- step 3: Set the component Spanning several rows or columns; after setting, you need to set a fill: android:layout_gravity = "fill"
- 4. Things to note when using GridLayout Place: Because GirdLayout was launched after 4.0, the minSDK version needs to be changed to version 14 or above. Otherwise, when writing layout code, something will go wrong inexplicably, saying that the GridLayout cannot be found. Of course, if you want compatibility with lower versions, you have to read the following!
The solution is very simple: just import the gridlayout package of the v7 package! The v7 package is generally in the sdk\extras\android\support\v7\gridlayout directory under sdk If you don’t have it, you can download it here: gridlayout_v7_jay.rar But when you use it, the label is written like this:
<android.support.v7.widget.GridLayout>`Summary of this section