Chinaunix首页 | 论坛 | 博客
  • 博客访问: 375633
  • 博文数量: 214
  • 博客积分: 770
  • 博客等级: 军士长
  • 技术积分: 1969
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-08 01:22
文章分类

全部博文(214)

文章存档

2013年(110)

2012年(104)

我的朋友

分类: Android平台

2013-03-27 16:37:58

简单记录
程序结构为 activity  main.xml   cell.mxl   另外drawalbe文件夹下放置所需图片资源

主UI布局代码:

点击(此处)折叠或打开

  1. <LinearLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical">"

  6.     
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="wrap_content"
  9.         android:id="@+id/grid01"
  10.         android:horizontalSpacing="1pt"
  11.         android:verticalSpacing="1pt"
  12.         android:numColumns="4"
  13.         android:gravity="center"
  14.          />
  15.     
  16.         android:id="@+id/imageview"
  17.         android:layout_width="240dp"
  18.         android:layout_height="240dp"
  19.         android:layout_gravity="center_horizontal"
  20.         android:contentDescription="@string/app_name
Activity代码

点击(此处)折叠或打开

  1. package com.gridviewdemo;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;



  6. import android.os.Bundle;
  7. import android.app.Activity;
  8. import android.view.Menu;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.AdapterView.OnItemClickListener;
  12. import android.widget.GridView;
  13. import android.widget.ImageView;
  14. import android.widget.SimpleAdapter;

  15. public class MainActivity extends Activity {
  16.     
  17.     GridView grid;
  18.     ImageView imageview;
  19.     int [] imageIds =new int []{
  20.             R.drawable.img1,R.drawable.img2,
  21.             R.drawable.img3,R.drawable.img4,
  22.             R.drawable.img5,R.drawable.img6,
  23.             R.drawable.img6,R.drawable.ic_launcher
  24.     };

  25.     @Override
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.main);
  29.         
  30.         List<Map<String,Object>> listitems =new ArrayList<Map<String,Object>>();
  31.         for(int i=0;i<imageIds.length;i++){
  32.             Map<String,Object> listitem =new HashMap<String,Object>();
  33.             listitem.put("image", imageIds[i]);
  34.             listitems.add(listitem);
  35.         }
  36.         
  37.         /**
  38.         * 本程序主要内容也是下面小段代码,所做的就是为网格布局构建一个Adapter
  39.         */
  40.         imageview = (ImageView)findViewById(R.id.imageview);
  41.         SimpleAdapter simpleAdapter =new SimpleAdapter(this,
  42.                  listitems,
  43.                  R.layout.cell,
  44.                  new String[]{"image"},
  45.                  new int []{R.id.image1});
  46.         grid =(GridView)findViewById(R.id.grid01);
  47.         grid.setAdapter(simpleAdapter);
  48.         
  49.         imageview.setImageResource(imageIds[7]);//默认显示的图片
  50.         
  51.         grid.setOnItemClickListener(new OnItemClickListener(){

  52.             @Override
  53.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  54.                     long arg3) {
  55.                 // TODO Auto-generated method stub
  56.                 imageview.setImageResource(imageIds[arg2]);
  57.                 
  58.             }
  59.             
  60.         });
  61.         
  62.         
  63.     }

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

  70. }
另外Adapter所需的cell.xml文件内容为

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="horizontal"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     android:gravity="center_horizontal"
  7.     android:padding="2pt"
  8.     >
  9. <ImageView
  10.     android:id="@+id/image1"
  11.     android:layout_width="50dp"
  12.     android:layout_height="50dp"
  13.     />    
  14. </LinearLayout>

运行效果为:

阅读(3413) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~