现在的位置: 首页 > 综合 > 正文

Android之 GridView学习笔记

2013年11月04日 ⁄ 综合 ⁄ 共 2307字 ⁄ 字号 评论关闭

cell.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" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/psb20" />

</LinearLayout>

activity_main.xml
<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=".MainActivity" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:numColumns="2" >
    </GridView>

</RelativeLayout>

package com.example.lesson20_gridviewdelegate1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.GridView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

private int Ids[] = {R.drawable.psb20,R.drawable.psb11,R.drawable.psb14,R.drawable.psb27,R.drawable.psb31,R.drawable.psb32,R.drawable.psb33,R.drawable.psb36};


private GridView gView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gView = (GridView)findViewById(R.id.gridView1);

List<Map<String, Object>> cellList = new ArrayList<Map<String,Object>>();
for (int i = 0; i < Ids.length; i++) {
Map<String, Object> cellMap = new HashMap<String, Object>();
cellMap.put("imagelog", Ids[i]);
cellList.add(cellMap);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, cellList, R.layout.cell, new String[]{"imagelog"}, new int[]{R.id.imageView1});
gView.setAdapter(simpleAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

抱歉!评论已关闭.