Android layout case for everyone Android nine lattice _android

Source: Internet
Author: User

Everyone android is a good mobile phone application software, we use the time to find his home page layout is nine mode, people feel very chic, because now a lot of Android software rarely use this layout mode, everyone Android use is very successful, Make people feel simple and generous and beautiful, let's look at the layout of Android for everyone

In fact, this layout is called "GridView" table layout, I would like to tell you:

First of all, please understand the concept of "iterative display", this is like layout nesting, we repeat in a large layout with a small layout of the same layout, those repeating part is composed of pictures and text of the small control, the picture above, text in the bottom, Then we just need to iterate these small controls into the main container.

Let's take a look at the actual rendering:

Let's take a look at the layout of the main container (GridView)
Main.xml

<?xmlversion= "1.0" encoding= "Utf-8"?> <gridviewxmlns:android=
"http://schemas.android.com/apk/res/" Android "
   android:id=" @+id/gridview "
   android:layout_width=" fill_parent "
   android:layout_height=" fill _parent "
   android:numcolumns=" Auto_fit "
   android:columnwidth=" 90DP "
   android:stretchmode=" columnWidth "
   android:gravity=" center >
</GridView>

Introduce some of the properties inside:

android:numcolumns= "Auto_fit ", The number of columns in the GridView is set to Automatic
android:columnwidth= "90DP ", the width of each column, which is the width of the item
android:stretchmode= "ColumnWidth", Zoom to sync with column width size

The attribute to be concerned here is ColumnWidth, which specifies the width of the column, a Column object, and a "repeatable subkey", which is the part of our picture item and the text below the picture. If you do not specify this width, the default is for each row (the displayed row, interface) only displays a "repeatable subkey", and when the width is specified, this article specifies 90DP, and if the actual row size of each row is greater than 90, he will continue to place the next "repeatable subkey" on the bank. This renders a single line showing multiple subkeys. NumColumns property, specifying an automatically populated value that indicates that the row is automatically populated.

Next we need to create an XML layout file where we write the subkey that needs to be "iterated" (relativelayout)

Unfamiliar with relativelayout layout please see my last blog: Android layout Basics and examples (i)
Item.xml

<?xmlversion= "1.0" encoding= "Utf-8"?> <relativelayoutxmlns:android=
"http://schemas.android.com/apk" /res/android "
android:layout_width=" fill_parent "
android:layout_height=" wrap_content "
>
<imageview
  android:layout_width= "wrap_content"
  android:id= "@+id/itemimage"
  android:layout_ height= "Wrap_content"
  android:layout_centerhorizontal= "true"/>
<textview
  android:layout_ Width= "Wrap_content"
  android:layout_height= "wrap_content"
  android:layout_below= "@+id/itemimage"
  android:id= "@+id/itemtext"
  android:layout_centerhorizontal= "true"
/>
</ Relativelayout>

Here a relative layout is used, and the attribute android:layout_below= "@+id/itemimage" is used in TextView to indicate that the text is below the picture.

In the final step, we need to put these things together and realize

Here we adopt the data structure in Java: HashMap, usage here is not much said, can own Baidu.

The ArrayList is then constructed as a data source, and then the Simpleadapter is constructed as a data adapter to specify the adapter object for the GridView.

Here is the Java code:
Layout_gridview.java

packagejiabin.activity;
Importjava.util.ArrayList;
 
Importjava.util.HashMap;
importandroid.app.Activity;
Importandroid.os.Bundle;
Importandroid.view.View;
Importandroid.widget.AdapterView;
Importandroid.widget.AdapterView.OnItemClickListener;
Importandroid.widget.GridView;
Importandroid.widget.SimpleAdapter;
 
Importandroid.widget.Toast; publicclasslayout_gridviewextendsactivity {/** Called when the ' activity is ' A-created @Override Publicvoidon
    Create (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    GridView GridView = (GridView) Findviewbyid (R.id.gridview);
    Arraylist 

Click event Response using toast, we can use this method to display the user clicks the effect and triggers the action, the above code uses the toast default effect,

Let's look at the effect chart:


This layout is not very windy, I hope this article will help you learn about Android software programming.

Related Article

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.