gridview显示图片

  1. public class MainActivity extends Activity {  
  2.   
  3.     private GridView gridView;  
  4.   
  5.     private int res[] = new int[] {R.drawable.an01,  
  6.             R.drawable.an02, R.drawable.an03, R.drawable.an01, R.drawable.an02,  
  7.             R.drawable.an03,R.drawable.an01,  
  8.             R.drawable.an02, R.drawable.an03, R.drawable.an01, R.drawable.an02,  
  9.             R.drawable.an03,R.drawable.an01,  
  10.             R.drawable.an02, R.drawable.an03, R.drawable.an01, R.drawable.an02,  
  11.             R.drawable.an03,R.drawable.an01,  
  12.             R.drawable.an02, R.drawable.an03, R.drawable.an01, R.drawable.an02,  
  13.             R.drawable.an03 };  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.           
  20.         gridView = (GridView) findViewById(R.id.gridView1);  
  21.           
  22.           
  23.          List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();  
  24.            
  25.          for(int i=0;i<res.length;i++){  
  26.          Map<String,Object> map = new HashMap<String, Object>();  
  27.             map.put("imageView",res[i]);  
  28.               
  29.             data.add(map);  
  30.          }  
  31.                    
  32.         SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,R.layout.activity_cell , new String[]{"imageView"}, new int[]{R.id.imageView1});  
  33.           
  34.         gridView.setAdapter(simpleAdapter);  
  35.           
  36.           
  37.           
  38.         //思考题 在Gridview中 SimpleAdpater  图片添加事件  
  39.           
  40.         gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  41.   
  42.             @Override  
  43.             public void onItemClick(AdapterView<?> parent, View view,  
  44.                     int position, long id) {  
  45.                 //可以显示图片大图  
  46.                 Toast.makeText(getApplicationContext(), "xxxx"+res[position], Toast.LENGTH_LONG).show();  
  47.                   
  48.             }  
  49.         });  
  50.           
  51.           
  52.     }  
  53.   
  54.     @Override  
  55.     public boolean onCreateOptionsMenu(Menu menu) {  
  56.         // Inflate the menu; this adds items to the action bar if it is present.  
  57.         getMenuInflater().inflate(R.menu.main, menu);  
  58.         return true;  
  59.     }  
  60.   
  61. }  


activity_main.xml

[java]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <GridView  
  12.         android:id="@+id/gridView1"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentLeft="true"  
  16.         android:layout_alignParentTop="true"  
  17.         android:numColumns="5" >  
  18.     </GridView>  
  19.   
  20. </RelativeLayout>  


activity_cell.xml

[java]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="wrap_content"  
  3.     android:layout_height="wrap_content"  
  4.     android:orientation="horizontal" >  
  5.   
  6.     <ImageView  
  7.         android:id="@+id/imageView1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:src="@drawable/ic_launcher" />  
  11.       
  12. </LinearLayout>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值