GridLayout (Grid layout)

Source: Internet
Author: User
Preface:

As a new layout after Android 4.0, the Tablelayout (table layout) described earlier is actually a little bit similar;

But something new has been added.

① As with linearlayout (linear layout), he can set the alignment of the components in the container

② components in a container can span multiple rows or across multiple columns (compared to a single line of Tablelayout)

Because Android 4.0 is new, API level 14, prior to this version of the SDK

Need to import the project, and so on, will detail


Common Properties: Arrange Alignment:

① Set How components are arranged: android:orientation= "" Vertical (vertical, default) or horizontal (horizontal)

② to set the alignment of the component: android:layout_gravity= "" Center,left,right,buttom Ah, these, if you want to use both kinds of words: eg:buttom|left


set the layout to a few rows of columns:

① How many lines are set: android:rowcount= "4"//Set Grid layout with 4 rows

② How many columns are set: android:columncount= "4"//Set Grid layout with 4 columns


set a component in a few rows of columns

Note: It's all counted from 0 onwards.


① component in the first line: Android:layout_row = "1"//Set component on the second row

② components in the number of columns: Android:layout_column = "2"//Set the component in the third column


set a component across several rows of columns:

① across several lines: Android:layout_rowspan = "2"//vertically across 2 rows

② across several columns: Android:layout_columnspan = "3"//Cross across 2 columns


usage Examples: The most common example----the calculator interface:

Effect Diagram:



PS: To speak here, the grid layout differs from other layouts, and you can set Layout_width and Layout_height properties for components

Because the width of the component is determined by a few rows of columns, you can also write a wrap_content


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_he ight= "Wrap_content" android:rowcount= "6" android:columncount= "4" android:orientation= "Horizontal" > &lt ; TextView android:layout_columnspan= "4" android:text= "0" android:textsize= "50sp" android:l ayout_marginleft= "5DP" android:layout_marginright= "5DP"/> <button android:text= "fallback" an
    	Droid:layout_columnspan= "2" android:layout_gravity= "Fill"/> <button android:text= "Empty" Android:layout_columnspan= "2" android:layout_gravity= "Fill"/> <button android:text= "+ "/> <button android:text=" 1 "/> <button android:text=" 2 "/&gt
    ;    
   <button android:text= "3" /> <button android:text= "-"/> <button android:text= "4"/> <but Ton 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= "/"/> <butto    
    n android:layout_width= "wrap_content" android:text= "." /> <button android:text= "0"/> <button android:text= "="/> </gridla
 Yout>

Code Explanation:

The code is simple, that is, the purge and fallback buttons are set across two columns, others are added directly

Each component is a row by default, one column

Here's one thing to note:

The number of rows and columns traversed by the component is indicated by Android:layout_rowspan and Android:layout_columnspan settings

Again: android:layout_gravity = "Fill" setting indicates that the component fills the entire row or column


Summary of Usage:

①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: First define the way the component is android:orientation horizontal or vertical

Step 2: Set the row or column where the component is located, remember that it was counted from 0

Step 3: Set the component across several rows or columns; After Setup, you need to set a fill: android:layout_gravity = "Fill"


problems you may encounter:

When the reader sets the layout to GridLayout, there will be an inexplicable error,

If the code syntax is not wrong, it could be a problem with the configuration file androidmanifest.xml

Because GridLayout was launched after Android 4.0, the API level is 14

Only need to change the MINSDK in the configuration file to 14 or later , save, the problem is solved.
How to use GridLayout in the Low version SDK

In fact, only need to guide a package can:

Download V7 This package

Import: GridLayout directory under SDK: Sdk\extras\android\support\v7\gridlayout

Then add the library to the project you need to use


Some beginners will not be able to pour the package, so here is a demonstration of the guide package process:


Found: Sdk\extras\android\support\v7\gridlayout, after clicking OK


As the figure, the library exists (PS: Because the author SDK is 4.2, so the exclamation mark will be displayed.)

Then add it into the project:




After selection:



Indicates that the add succeeds, and then in the XML file we can directly use the

in XML as long as the GridLayout is written:

<android.support.v7.widget.GridLayout>

and add a row of namespaces instead of replacing them:

xmlns:app= "Http://schemas.android.com/apk/res-auto"

Can

Give the download link for the V7 package:

http://pan.baidu.com/s/1kTC2s3l


Summary:

The use of GridLayout is not complicated, this section is written here

In order to take care of some beginners, a bit verbose, forget readers forgive me

Thank you for your support, if there are any flaws, forget the reader to give, or have any interesting examples

Also can and the author say, O (∩_∩) o Thank you.

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.