标签:
xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.gridviewdemo.MainActivity" > <!-- GridView是可滚动的网格,一般用来显示多张图片。 android:horizontalSpacing="10dp"两列之间的间距是10dp android:numColumns="auto_fit"自动分配(安卓自提供),也可些一行显示几个 android:verticalSpacing="10dp" 两行之间的间距是10dp android:stretchMode="spacingWidth"缩放与列宽大小同步 --> <GridView android:id="@+id/gridView_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_horizontal" android:horizontalSpacing="10dp" android:numColumns="3" android:verticalSpacing="10dp" > </GridView> <!-- 运行效果中,各列之间的间距大于10dp,是因为将列数设为了3行,因此会平均分布 --> </LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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"> <ImageView android:id="@+id/image" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/address_book" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:layout_marginTop="5dp" android:text="名字"/> </LinearLayout>
源代码:
package com.example.gridviewdemo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; 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; /* * GridView:以以网格形式显示页面 * 创建GridView的步骤: * 1.准备数据源 * 2.新建适配器(SimpleAdapter) * 3、GridView加载适配器 * 4.Gridview配置事件监听器(OnItemClickListener) */ public class MainActivity extends Activity implements OnItemClickListener { private GridView gridView; private SimpleAdapter adapter; private List<Map<String, Object>> list; private String[] iconNames; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (GridView) findViewById(R.id.gridView_1); list = new ArrayList<Map<String, Object>>(); adapter = new SimpleAdapter(this, getData(), R.layout.item, new String[] { "图片", "文字" }, new int[] { R.id.image, R.id.text }); gridView.setAdapter(adapter); gridView.setOnItemClickListener(this);//设置监听器 } private List<Map<String, Object>> getData() { iconNames = new String[] { "联系人", "日历", "照相机", "时钟", "游戏", "短信", "铃声", "设置", "语音", "天气", "浏览器", "YouTube" }; int[] drawable = { 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 }; for (int i = 0; i < iconNames.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("图片", drawable[i]); map.put("文字", iconNames[i]); list.add(map); } return list; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(this,iconNames[position], Toast.LENGTH_SHORT).show(); Log.i("Toast", iconNames[position]); } }
标签:
原文地址:http://www.cnblogs.com/fangg/p/5583056.html