Android implements the Application List on the main interface of jiugongge

Source: Internet
Author: User

The original version is implemented in the Android app

In this article, you need to add/modify three files: Main. XML, meunitem. XML, and Java.Source code.

Main. xml SourceCodeAs follows, It is a girdview, used to load items:

<? XML version = "1.0" encoding = "UTF-8"?>
<Gridview xmlns: 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: horizontalspacing = "10dp"
Android: verticalspacing = "10dp"
Android: columnwidth = "90dp"
Android: stretchmode = "columnwidth"
Android: gravity = "center"> </gridview>

Some attributes are described as follows:

Android: numcolumns = "auto_fit", set the number of columns in the gridview to automatic

Android: columnwidth = "90dp", the width of each column, that is, the width of the item
Android: stretchmode = "columnwidth", scaling and column width Synchronization
Android: verticalspacing = "10dp", the margin between two rows, for example, Row 1 (no.0 ~ No. 2) and row 2 (No. 3 ~ No. 5) 10dp spacing
Android: horizontalspacing = "10dp", margin between two columns.

Next we will introduce meunitem. XML, which is similar to the imageitem. XML in the previous listview:

<? XML version = "1.0" encoding = "UTF-8"?>
<Relativelayout xmlns: 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>

Finally, the source code of Java

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. mainmenu );

Gridview = (gridview) findviewbyid (R. Id. gridview );
Arraylist

For (INT I = 1; I <10; I ++ ){
Hashmap <string, Object> map = new hashmap <string, Object> ();
Map. Put ("itemimage", R. drawable. Icon );
Map. Put ("itemtext", "No." + I );
Meumlist. Add (MAP );
}

Simpleadapter samenuitem = new simpleadapter (this,
Meumlist, // Data Source
R. layout. menuitem, // XML implementation
New String [] {"itemimage", "itemtext"}, // key of the corresponding map
New int [] {R. Id. itemimage, R. Id. itemtext}); // ID of the corresponding R

// Add an item to the grid
gridview. setadapter (samenuitem);
gridview. setonitemclicklistener (New onitemclicklistener () {
Public void onitemclick (adapterview Arg0, view arg1, int arg2, long arg3) {
system. out. println ("Click index:" + arg2);
}< BR >}< br> );
}< br>
attached

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.