105934587  

mian.xml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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:orientation="vertical"
    >
    <!--定义一个4列的GridView控件  -->
    <GridView
        android:id="@+id/gridView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:numColumns="4" >
    </GridView>
    <!-- 定义一个ImageView -->
    <ImageView
        android:id="@+id/main_im"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/bomb1"
        android:layout_weight="1.5" />
</LinearLayout>

 

在定义一个cell.xml文件,里面只有一个ImageView。

1
2
3
4
5
6
7
8
9
10
11
<?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="50dip"
        android:layout_height="50dip"
        />
</LinearLayout>

 

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class MainActivity extends Activity {
    /**
     * @author shao
     *使用SimpleAdapter为GridView控件添加内容
     */
    private GridView gd;
    private ImageView imageView;
    private SimpleAdapter simpleAdapter;
    private int[] imageId = { R.drawable.bomb1, R.drawable.bomb2,
            R.drawable.bomb3, R.drawable.bomb4, R.drawable.bomb5,
            R.drawable.bomb6, R.drawable.bomb7, R.drawable.bomb8,
            R.drawable.bomb9, R.drawable.bomb10, R.drawable.bomb11,
            R.drawable.bomb12};
    private List<Map<String, Object>> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //查找控件
        gd = (GridView) findViewById(R.id.gridView1);
        imageView = (ImageView) findViewById(R.id.main_im);   
        //为list添加内容
        list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < imageId.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", imageId[i]);
            list.add(map);
        }
        simpleAdapter = new SimpleAdapter(this, list, R.layout.cell,
                new String[] { "image" }, new int[] { R.id.imageView1 });
        //为GridView控件添加Adapter
        gd.setAdapter(simpleAdapter);
        //为GridView控件添加监听
        gd.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                imageView.setImageResource(imageId[position]);
            }
        });
    }
}

 

 

  

 

 

本文出自 “天马行空” 博客,请务必保留此出处http://shaotao.blog.51cto.com/7606729/1281314

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()