GridView

 

<?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" >

    <GridView
        android:id="@+id/gridview"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:horizontalSpacing="10dp"
        android:numColumns="4"
        android:layout_marginTop="30dp"
        android:verticalSpacing="10dp" >
    </GridView>

</LinearLayout>
<?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:background="#000000"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="50dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:text="文字"
        android:textColor="#ffffff"
        android:textSize="15dp" />

</LinearLayout>

 

 

// 1准备数据源

private int[] icon = { R.drawable.ic_books, R.drawable.ic_browser,
            R.drawable.ic_calculator, R.drawable.ic_calendar,
            R.drawable.ic_camera, R.drawable.ic_messages,
            R.drawable.ic_contacts, R.drawable.ic_gallery, R.drawable.ic_mail,
            R.drawable.ic_maps, R.drawable.ic_market,
            R.drawable.ic_moviestudio, R.drawable.ic_music, R.drawable.ic_talk,
            R.drawable.ic_phone, R.drawable.ic_settings };
    private String[] iconName = { "阅读器", "浏览器", "计算器", "日历", "照相机", "短信", "协议",
            "画廊", "邮件", "地图", "市场", "电影院", "音乐", "语音", "拨号", "设置" };

// 2新建适配器(SimpleAdapter)

// 3GridView加载适配器
// 4GridView配置时间监听器,(OnItemOnClickListener)

public class MainActivity extends Activity implements OnItemClickListener {
    private GridView gridView;
    private List<Map<String, Object>> datalist;
    private SimpleAdapter simpleAdapter;
    private int[] icon = { R.drawable.ic_books, R.drawable.ic_browser,
            R.drawable.ic_calculator, R.drawable.ic_calendar,
            R.drawable.ic_camera, R.drawable.ic_messages,
            R.drawable.ic_contacts, R.drawable.ic_gallery, R.drawable.ic_mail,
            R.drawable.ic_maps, R.drawable.ic_market,
            R.drawable.ic_moviestudio, R.drawable.ic_music, R.drawable.ic_talk,
            R.drawable.ic_phone, R.drawable.ic_settings };
    private String[] iconName = { "阅读器", "浏览器", "计算器", "日历", "照相机", "短信", "协议",
            "画廊", "邮件", "地图", "市场", "电影院", "音乐", "语音", "拨号", "设置" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gridView = (GridView) findViewById(R.id.gridview);
      
        datalist = new ArrayList<Map<String, Object>>();// 数据源的集合申明出来

        simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item,
                new String[] { "img", "text" },
                new int[] { R.id.img, R.id.text });
        gridView.setAdapter(simpleAdapter);
        gridView.setOnItemClickListener(this);

    }

    private List<Map<String, Object>> getData() {
        // TODO Auto-generated method stub
        for (int i = 0; i < icon.length; i++) {
//按照数组的长度,把里面的信息全都输出出来。 Map
<String, Object> map = new HashMap<String, Object>(); map.put("img", icon[i]); map.put("text", iconName[i]); datalist.add(map); } return datalist; // 要是返回datalist,则 为List<Map<String, Object>> } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, iconName[position], Toast.LENGTH_SHORT).show(); }// iconName[position]就是脚注 }

posted @ 2016-08-19 12:54  一米阳光!  阅读(218)  评论(0编辑  收藏  举报