Implementation of Android jiugongge

Source: Internet
Author: User

1. xml code:

Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_weight = "1.0"
Android: background = "@ drawable/yellow"
>
<ImageView android: id = "@ + id/ImageView01"
Android: layout_width = "100sp"
Android: layout_height = "100sp"
Android: layout_gravity = "center_vertical"
Android: background = "@ drawable/a"> </ImageView>
<GridView
Android: id = "@ + id/gridview"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: numColumns = "3"
Android: verticalSpacing = "30dip"
Android: horizontalSpacing = "10dip"
Android: columnWidth = "90dip"
Android: stretchMode = "columnWidth"
Android: gravity = "center"
Android: listSelector = "@ drawable/c"
>
</GridView>
</LinearLayout>

Android: numColumns = "3" indicates automatic when the number of columns in the nine cells is auto_fit.

2. Implementation Code

Copy codeThe Code is as follows: public class MainActivity extends Activity {

/** Called when the activity is first created .*/

@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
// Set no screen title
This. requestWindowFeature (Window. FEATURE_NO_TITLE );
// Remove the title bar
This. getWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,
WindowManager. LayoutParams. FLAG_FULLSCREEN );
SetContentView (R. layout. activity_main );

GridView gridview = (GridView) findViewById (R. id. gridview );
// Create an array list object
ArrayList <HashMap <String, Object> lstImageItem = new ArrayList <HashMap <String, Object> ();

/**
* Add content for each grid
*/
For (int I = 1; I <10; I ++ ){
HashMap <String, Object> map = new HashMap <String, Object> (); // create a hashmap Object

If (I = 1 ){
Map. put ("ItemImage", R. drawable. g11 );
Map. put ("ItemText", getResources ()
. GetString (R. string. gridview1 ));
}

If (I = 2 ){
Map. put ("ItemImage", R. drawable. g12 );
Map. put ("ItemText", getResources ()
. GetString (R. string. gridview2 ));
}

If (I = 3 ){
Map. put ("ItemImage", R. drawable. g13 );
Map. put ("ItemText", getResources ()
. GetString (R. string. gridview3 ));
}

If (I = 4 ){
Map. put ("ItemImage", R. drawable. g14 );
Map. put ("ItemText", getResources ()
. GetString (R. string. gridview4 ));
}

If (I = 5 ){
Map. put ("ItemImage", R. drawable. g15 );
Map. put ("ItemText", getResources ()
. GetString (R. string. gridview5 ));
}

If (I = 6 ){
Map. put ("ItemImage", R. drawable. g16 );
Map. put ("ItemText", getResources ()
. GetString (R. string. gridview6 ));
}

If (I = 7 ){
Map. put ("ItemImage", R. drawable. g17 );
Map. put ("ItemText", getResources ()
. GetString (R. string. gridview7 ));
}

If (I = 8 ){
Map. put ("ItemImage", R. drawable. g18 );
Map. put ("ItemText", getResources ()
. GetString (R. string. gridview8 ));
}

If (I = 9 ){
Map. put ("ItemImage", R. drawable. g19 );
Map. put ("ItemText", getResources ()
. GetString (R. string. gridview9 ));
}

LstImageItem. add (map );
}

/**
* Create a SimpleAdapter adapter for the GridView
*/
// The five parameters in SimpleAdapter () are: the first context, the second data resource, the layout file of the third sub-item, and the Key array of the fourth sub-item.
// The Value array in each fifth subitem
SimpleAdapter saImageItems = new SimpleAdapter (this, lstImageItem,
R. layout. grid_item, new String [] {"ItemImage", "ItemText "},
New int [] {R. id. ItemImage, R. id. ItemText });
Gridview. setAdapter (saImageItems); // Add an adapter
Gridview. setOnItemClickListener (new ItemClickListener (); // set a listener for each subitem
}

Class ItemClickListener implements OnItemClickListener {
@ SuppressWarnings ("unchecked ")
Public void onItemClick (AdapterView <?> Arg0, // The AdapterView where
// Click happened
View arg1, // The view within the AdapterView that was clicked
Int arg2, // The position of the view in the adapter
Long arg3 // The row id of the item that was clicked
){
HashMap <String, Object> item = (HashMap <String, Object>) arg0
. GetItemAtPosition (arg2 );
If (item. get ("ItemText"). equals (
GetResources (). getString (R. string. gridview1 ))){
Toast. makeText (MainActivity. this, R. string. gridview1,
Toast. LENGTH_LONG). show ();
}

If (item. get ("ItemText"). equals (
GetResources (). getString (R. string. gridview2 ))){
Toast. makeText (MainActivity. this, R. string. gridview2,
Toast. LENGTH_LONG). show ();
}

If (item. get ("ItemText"). equals (
GetResources (). getString (R. string. gridview3 ))){
Toast. makeText (MainActivity. this, R. string. gridview3,
Toast. LENGTH_LONG). show ();
}

If (item. get ("ItemText"). equals (
GetResources (). getString (R. string. gridview4 ))){
Toast. makeText (MainActivity. this, R. string. gridview4,
Toast. LENGTH_LONG). show ();
}

If (item. get ("ItemText"). equals (
GetResources (). getString (R. string. gridview5 ))){
Toast. makeText (MainActivity. this, R. string. gridview5,
Toast. LENGTH_LONG). show ();
}

If (item. get ("ItemText"). equals (
GetResources (). getString (R. string. gridview6 ))){
Toast. makeText (MainActivity. this, R. string. gridview6,
Toast. LENGTH_LONG). show ();
}

If (item. get ("ItemText"). equals (
GetResources (). getString (R. string. gridview7 ))){
Toast. makeText (MainActivity. this, R. string. gridview7,
Toast. LENGTH_LONG). show ();
}

If (item. get ("ItemText"). equals (
GetResources (). getString (R. string. gridview8 ))){
Toast. makeText (MainActivity. this, R. string. gridview8,
Toast. LENGTH_LONG). show ();
}

If (item. get ("ItemText"). equals (
GetResources (). getString (R. string. gridview9 ))){
Toast. makeText (MainActivity. this, R. string. gridview9,
Toast. LENGTH_LONG). show ();
}
}
}

}

3. Implementation results

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.