Android: Use instance of control GridView _android

Source: Internet
Author: User

If it is a list (single row multiline form) the use of ListView, if it is a multiline multiple-column mesh form of precedence for the GridView.

<?xml version= "1.0" encoding= "Utf-8"?> <gridview xmlns:android=
"http://schemas.android.com/apk/res/" Android "
  android:layout_width=" match_parent "
  android:layout_height=" match_parent ">
  

</ Gridview>

Some properties of Girdview:

    • android:numcolumns= "Auto_fit"--------the number of columns is set to Automatic
    • Android:columnwidth= "90DP",----------the width of each column, i.e. the width of the item
    • Android:stretchmode= "columnWidth"------Zoom and column size sync
    • android:verticalspacing= "10DP"----------vertical margins
    • android:horizontalspacing= "10DP"-------horizontal margin

1, prepare the data source

2. New Adapter

3. Load Adapter

The GridView (grid view) is the way to display content, generally used to display pictures, pictures and other content, such as the implementation of nine Bongtu, with the GridView is the first choice, is also the simplest, the following example,

Instance Download: Demo

Effect Chart:

Mainactivity.java

Package Com.example.testgridview;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;

Import Java.util.Map;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.GridView;

Import Android.widget.SimpleAdapter;
  public class Mainactivity extends activity {private GridView Gview;
  Private list<map<string, object>> data_list;
  Private Simpleadapter Sim_adapter; Picture encapsulated as an array private int[] icon = {r.drawable.address_book, R.drawable.calendar, R.drawable.camera, r.drawable.cl Ock, R.drawable.games_control, R.drawable.messenger, R.drawable.ringtone, R.drawable.settings, R.drawable.spee
  Ch_balloon, R.drawable.weather, R.drawable.world, r.drawable.youtube};

  Private string[] Iconname = {"Address book", "Calendar", "Camera", "clock", "Game", "SMS", "Ringtone", "Settings", "Voice", "Weather", "browser", "video"};
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layoUt.test);
    Gview = (GridView) Findviewbyid (R.id.gview);
    Create a new list data_list = new arraylist<map<string, object>> ();
    Get Data getData ();
    New Adapter String [] from ={"image", "text"};
    int [] to = {R.id.image,r.id.text};
    Sim_adapter = new Simpleadapter (this, data_list, R.layout.item, from, to);
  Configure Adapter Gview.setadapter (Sim_adapter); Public list<map<string, Object>> GetData () {//cion and iconname are of the same length, either of which is optional for (int i=
      0;i<icon.length;i++) {map<string, object> Map = new hashmap<string, object> ();
      Map.put ("image", icon[i]);
      Map.put ("text", Iconname[i]);
    Data_list.add (map);
  return data_list;

 }
  

}

Test.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" match_parent "
  android:layout_height=" match_parent "
  android:o" rientation= "vertical" 
  android:background= "#000"
  >
  
<gridview 
    android:id= "@+id/gview"
    Android:layout_width= "Match_parent"
    android:layout_height= "wrap_content"
    android:numcolumns= "Auto_" Fit "  
    android:columnwidth=" 80DP "
    android:stretchmode=" ColumnWidth "
    ></GridView>
</LinearLayout>

Item.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" wrap_content "
  android:layout_height=" wrap_content "
  android:o" rientation= "vertical" 
  android:gravity= "center"
  android:padding= "10DP"
  >
  
  
<imageview 
  android:src= "@drawable/ic_launcher"
  android:id= "@+id/image"
  android:layout_width= "60DP"
  Android : layout_height= "60DP"
  
  />

<textview 
  android:id= "@+id/text"
  5DP "
  android:layout_width=" wrap_content "
  android:layout_height=" wrap_content "
  Android:textcolor = "#ffffff"
  android:text= "text"
  />
</LinearLayout>

Listener interface: Onitemclicklistener

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.