这是java代码: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.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Adapter;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.GridView;import android.widget.SimpleAdapter;import android.widget.Toast;public class MainActivity extends Activity implements OnItemClickListener{ private GridView gridView; private List<Map<String, Object>> dataList; private int[] icon = {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}; private String [] iconName = {"地址簿","日历","相机","闹铃","游戏","信息","音乐","设置","通讯录","天气","互联网","YouTube"}; private SimpleAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView=(GridView) findViewById(R.id.gridView); //1.主被数据源 //2.新建适配器(simpleAdapter) //3.GridView加载适配器 //4.GridView配置事件监听器(OnItemClickListener) dataList = new ArrayList<Map<String,Object>>(); getData(); adapter = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic","text"}, new int[]{R.id.pic,R.id.text}); gridView.setAdapter(adapter); } private List<Map<String, Object>> getData() { for (int i=1;i<icon.length;i++){ Map<String, Object>map= new HashMap<String, Object>(); map.put("pic", icon[i]); map.put("text", iconName[i]); } return dataList; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Toast.makeText(this, "我是"+iconName[position], Toast.LENGTH_SHORT).show(); }}这是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/pic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/text" android:layout_marginTop="5dp" android:textColor="#ffffff" android:text="文字" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout><RelativeLayout 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.android_gridview.MainActivity" > <!-- android:numColumns="3" 每一行显示多少列 android:horizontalSpacing="" 两列之间的间距 android:verticalSpacing="" 两行之间的间距 --> <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:numColumns="3" > </GridView></RelativeLayout>
1 回答
- 1 回答
- 0 关注
- 840 浏览
添加回答
举报
0/150
提交
取消