- <?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="fill_parent">
- <GridView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/MyGridView"
- android:layout_alignParentTop="true"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="50dp"
- android:columnWidth="90dp"
- android:stretchMode="columnWidth"
- android:numColumns="auto_fit"
- android:horizontalSpacing="10dp"
- android:verticalSpacing="10dp"
- android:gravity="center">
- </GridView>
- <TextView android:id="@+id/MyTextView"
- android:layout_alignParentBottom="true"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textStyle="bold"
- android:text="基于GridView的菜单"
- android:textColor="@android:color/white"
- android:gravity="center" />
- </RelativeLayout>
- res/layout/item.xml
-
- <?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="wrap_content">
- <ImageView
- android:id="@+id/ItemImageView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center"/>
- <TextView
- android:id="@+id/ItemTextView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"/>
- </LinearLayout>
GridViewTestActivity.java
-
- package com.zeph.android.gridview;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.GridView;
- import android.widget.SimpleAdapter;
- import android.widget.Toast;
- public class GridViewTestActivity extends Activity {
- private GridView mGridView;
- private int[] imageRes = { R.drawable.png1, R.drawable.png2,
- R.drawable.png3, R.drawable.png4, R.drawable.png5, R.drawable.png6,
- R.drawable.png7, R.drawable.png8, R.drawable.png9 };
- private String[] itemName = { "PNG1", "PNG2", "PNG3", "PNG4", "PNG5",
- "PNG6", "PNG7", "PNG8", "PNG9" };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mGridView = (GridView) findViewById(R.id.MyGridView);
- List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
- int length = imageRes.length;
- for (int i = 0; i < length; i++) {
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("ItemImageView", imageRes[i]);
- map.put("ItemTextView", itemName[i]);
- data.add(map);
- }
- SimpleAdapter mSimpleAdapter = new SimpleAdapter(
- GridViewTestActivity.this, data, R.layout.item, new String[] {
- "ItemImageView", "ItemTextView" }, new int[] {
- R.id.ItemImageView, R.id.ItemTextView });
- mGridView.setAdapter(mSimpleAdapter);
- mGridView.setOnItemClickListener(new GridViewItemOnClick());
- }
- public class GridViewItemOnClick implements OnItemClickListener {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position,
- long id) {
- Toast.makeText(getApplicationContext(), position + "",
- Toast.LENGTH_SHORT).show();
- }
- }
- }
- 摘自http://blog.csdn.net/benweizhu/article/details/6737062
相关文章
- 08-22安卓开发笔记——多种方式实现底部菜单栏(仿微信界面)
- 08-22用纯css实现下拉菜单的几种方式
- 08-22python实现应用程序在右键菜单中添加打开方式
- 08-22Tip和菜单的实现方式
- 08-22JS实现连接方式的菜单
- 08-22Android利用GridView加载九宫格菜单
- 08-22GridView九宫格菜单实现方式
- 08-22项目一:第十二天 1、常见权限控制方式 2、基于shiro提供url拦截方式验证权限 3、在realm中授权 5、总结验证权限方式(四种) 6、用户注销7、基于treegrid实现菜单展示
- 08-22Android开发之再探底部菜单TabLayout与Bottom navigation实现方式
- 08-22全面总结侧滑菜单的5种实现方式