GridView九宫格菜单实现方式

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <GridView xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:id="@+id/MyGridView"
  7. android:layout_alignParentTop="true"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_marginTop="50dp"
  11. android:columnWidth="90dp"
  12. android:stretchMode="columnWidth"
  13. android:numColumns="auto_fit"
  14. android:horizontalSpacing="10dp"
  15. android:verticalSpacing="10dp"
  16. android:gravity="center">
  17. </GridView>
  18. <TextView android:id="@+id/MyTextView"
  19. android:layout_alignParentBottom="true"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:textStyle="bold"
  23. android:text="基于GridView的菜单"
  24. android:textColor="@android:color/white"
  25. android:gravity="center" />
  26. </RelativeLayout>
  27. res/layout/item.xml
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout
    3. xmlns:android="http://schemas.android.com/apk/res/android"
    4. android:orientation="vertical"
    5. android:layout_width="fill_parent"
    6. android:layout_height="wrap_content">
    7. <ImageView
    8. android:id="@+id/ItemImageView"
    9. android:layout_width="fill_parent"
    10. android:layout_height="wrap_content"
    11. android:layout_gravity="center"/>
    12. <TextView
    13. android:id="@+id/ItemTextView"
    14. android:layout_width="fill_parent"
    15. android:layout_height="wrap_content"
    16. android:gravity="center"/>
    17. </LinearLayout>

    GridViewTestActivity.java

    1. package com.zeph.android.gridview;
    2. import java.util.ArrayList;
    3. import java.util.HashMap;
    4. import java.util.List;
    5. import android.app.Activity;
    6. import android.os.Bundle;
    7. import android.view.View;
    8. import android.widget.AdapterView;
    9. import android.widget.AdapterView.OnItemClickListener;
    10. import android.widget.GridView;
    11. import android.widget.SimpleAdapter;
    12. import android.widget.Toast;
    13. public class GridViewTestActivity extends Activity {
    14. private GridView mGridView;
    15. private int[] imageRes = { R.drawable.png1, R.drawable.png2,
    16. R.drawable.png3, R.drawable.png4, R.drawable.png5, R.drawable.png6,
    17. R.drawable.png7, R.drawable.png8, R.drawable.png9 };
    18. private String[] itemName = { "PNG1", "PNG2", "PNG3", "PNG4", "PNG5",
    19. "PNG6", "PNG7", "PNG8", "PNG9" };
    20. @Override
    21. public void onCreate(Bundle savedInstanceState) {
    22. super.onCreate(savedInstanceState);
    23. setContentView(R.layout.main);
    24. mGridView = (GridView) findViewById(R.id.MyGridView);
    25. List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
    26. int length = imageRes.length;
    27. for (int i = 0; i < length; i++) {
    28. HashMap<String, Object> map = new HashMap<String, Object>();
    29. map.put("ItemImageView", imageRes[i]);
    30. map.put("ItemTextView", itemName[i]);
    31. data.add(map);
    32. }
    33. SimpleAdapter mSimpleAdapter = new SimpleAdapter(
    34. GridViewTestActivity.this, data, R.layout.item, new String[] {
    35. "ItemImageView", "ItemTextView" }, new int[] {
    36. R.id.ItemImageView, R.id.ItemTextView });
    37. mGridView.setAdapter(mSimpleAdapter);
    38. mGridView.setOnItemClickListener(new GridViewItemOnClick());
    39. }
    40. public class GridViewItemOnClick implements OnItemClickListener {
    41. @Override
    42. public void onItemClick(AdapterView<?> parent, View view, int position,
    43. long id) {
    44. Toast.makeText(getApplicationContext(), position + "",
    45. Toast.LENGTH_SHORT).show();
    46. }
    47. }
    48. }  
            1. 摘自http://blog.csdn.net/benweizhu/article/details/6737062
上一篇:centos7.2环境中kettle环境搭建及任务推送配置详解


下一篇:ASP.NET实现从服务器下载文件问题处理