The grid layout is a new layout manager for Android4.0, so it needs to be available after Android4.0, and the previous platform will need to be imported into the appropriate support library.
The GridLayout function is similar to the table tag in HTML, which divides the entire container into row*column meshes, each of which can place a component, or set how many columns and rows a component spans.
GridLayout provides the setrowcount (int) and setcolumncount (int) methods to control the number of rows and columns of the grid.
Simple code example:
Gridlayout.xml
<?XML version= "1.0" encoding= "Utf-8"?><GridLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:rowcount= "6"Android:columncount= "4"Android:id= "@+id/root"> <!--set a text box that spans four columns and sets the foreground color, background color, and other properties of the text box - <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_columnspan= "4"android:textsize= "50SP"Android:layout_marginleft= "4px"Android:layout_marginright= "4px"android:padding= "5px"android:layout_gravity= "Right"Android:background= "#eee"Android:textcolor= "#000"Android:text= "0" /> <!--Define a button that spans four columns - <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_columnspan= "4"Android:text= "Clear"/> <!--Add another button - <ButtonAndroid:text= "7"Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text= "8"Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text= "9"Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text="/"Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text= "4"Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text= "5"Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text= "6"Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text="*"Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text= "1"Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text= "2"Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text= "3"Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text="-"Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text= "0"Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text="."Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text="+"Android:layout_rowweight= "1"Android:layout_columnweight= "1"/> <ButtonAndroid:text="="Android:layout_rowweight= "1"Android:layout_columnweight= "1"/></GridLayout>
Operation Result:
Android Notes (10) layout in Android-Grid layout