Android control GridView Implementation Menu interface

Source: Internet
Author: User

Girdview for nine Gongge layout, add picture as text to make menu

Activiyy_main.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:background= "#000000">        <GridViewAndroid:id= "@+id/gridview"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:numcolumns= "3"android:horizontalspacing= "10DP"android:verticalspacing= "10DP"        ></GridView></LinearLayout>
Android:numcolumns               Set the number of columns in a row (parameter Auto_fit indicates adaptive)

Android:horizontalspacing= "" To set the horizontal spacing
Android:verticalspacing= ""       Set Vertical spacing

A single element of nine Gongge

Item.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"android:gravity= "Center"Android:background= "#000000"    >        <ImageViewAndroid:id= "@+id/image"Android:layout_width= "60DP"Android:layout_height= "60DP"android:src= "@drawable/ic_launcher"        />       <TextViewAndroid:id= "@+id/text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margintop= "5DP"Android:text= "text"Android:textcolor= "#ffffff"        /></LinearLayout>

Each element is composed of ImageView controls and TextView,

Android:layout_margintop= "" Sets the spacing between the top and ImageView of the TextView

Set the color and the background is black.

Mainactivity.java
Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.Window;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.GridView;ImportAndroid.widget.SimpleAdapter;ImportAndroid.widget.Toast; Public classMainactivityextendsActionbaractivityImplementsonitemclicklistener{PrivateGridView GridView; PrivateList<map<string, object>>DataList; Private int[] icon ={r.drawable.address_book, R.drawable.calendar, R.drawable.camera, R.drawable.clock, R.drawable            . Games_control, R.drawable.messenger, R.drawable.ringtone, R.drawable.settings, R.drawable.speech_balloon,    R.drawable.weather, R.drawable.world, r.drawable.youtube}; Privatestring[] Icon_name = {        Contacts, calendar, camera,         "Clock", "Game", "SMS",         "Ringtone", "Settings", "language",        "Weather", "browser", "video"    }; PrivateSimpleadapter Adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);                Setcontentview (R.layout.activity_main); GridView=(GridView) Findviewbyid (R.id.gridview); DataList=NewArraylist<map<string,object>>(); Adapter=NewSimpleadapter ( This, GetData (), R.layout.item,Newstring[]{"image", "Text"},New int[]{r.id.image, r.id.text}]; //adding adapters and listenersGridview.setadapter (adapter); Gridview.setonitemclicklistener ( This); }        PrivateList<map<string, object>>GetData () { for(inti = 0; i<icon.length; i++) {Map<string, object> map =NewHashmap<string, object>(); Map.put ("Image", Icon[i]); Map.put ("Text", Icon_name[i]);        Datalist.add (map); }        returnDataList; }    //Listening for click events@Override Public voidOnitemclick (adapterview<?> Parent, view view,intposition,LongID) {//displays a message specifying the length of time the message is displayedToast.maketext ( This, "I am" +Icon_name[position], toast.length_short). Show (); }    }

Picture resources into the corresponding drawable folder

The GridView corresponding adapter uses Simpleadapter, data with arraylist<map<string,object>> ();

The GetData () function assigns values to DataList, and the array icon and Icon_name store picture resources and string resources.

Android control GridView Implementation Menu interface

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.