标签:des android style blog http io ar color os
GridView(九宫格)在Android开发中和ListView一样经常被使用。如我们经常使用的快图浏览,里面就有将图片的布局改为网格(即GridView)的选项。
GridView
is a ViewGroup
that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a ListAdapter
网格视图是一个二维的可滚动的网格中展示网格项的视图集合。网格项使用ListAdapter自动插入到布局中。
MainActivity
1 package com.johntsai.gridviewdemo; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 6 import android.app.Activity; 7 import android.content.Intent; 8 import android.os.Bundle; 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.SimpleAdapter; 14 import android.widget.Toast; 15 16 public class MainActivity extends Activity { 17 private int ids[]={ 18 R.drawable.aaa, 19 R.drawable.bbb, 20 R.drawable.ccc, 21 R.drawable.ddd, 22 R.drawable.eee, 23 R.drawable.fff, 24 R.drawable.ggg, 25 R.drawable.hhh, 26 R.drawable.aaa 27 }; 28 private GridView gridView; 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 34 gridView = (GridView)findViewById(R.id.gridview); 35 36 ArrayList<HashMap<String, Object>> images = new ArrayList<HashMap<String,Object>>(); 37 for(int i=0;i<ids.length;i++){ 38 HashMap<String, Object>map = new HashMap<String, Object>(); 39 map.put("Image", ids[i]); 40 map.put("Id", "Android"+i+"号"); 41 images.add(map); 42 } 43 44 SimpleAdapter adapter = new SimpleAdapter(this, 45 images, R.layout.gridview_item, 46 new String[]{"Image","Id"}, new int[]{R.id.imageItem,R.id.idItem}); 47 gridView.setAdapter(adapter); 48 49 gridView.setOnItemClickListener(new OnItemClickListener() { 50 51 @Override 52 public void onItemClick(AdapterView<?> parent, View view, 53 int position, long id) { 54 Toast.makeText(getApplicationContext(), 55 "你点击了" + position+"号", Toast.LENGTH_LONG).show(); 56 57 } 58 }); 59 } 60 }
主界面布局文件
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:columnWidth="90dp" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:gravity="center" /> <!-- android:columnWidth="90dp" 列宽设置为90dp android:numColumns="auto_fit"列数设置为自动 android:stretchMode="columnWidth" 缩放与列的宽度同步 -->
GridView项布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="4dip"> 6 7 <ImageView 8 android:id="@+id/imageItem" 9 android:layout_height="70dp" 10 android:layout_width="70dp" 11 android:layout_centerHorizontal="true" 12 /> 13 <TextView 14 android:id="@+id/idItem" 15 android:layout_height="wrap_content" 16 android:layout_width="70dp" 17 android:layout_centerHorizontal="true" 18 android:layout_below="@id/imageItem" 19 /> 20 </RelativeLayout>
运行效果:
标签:des android style blog http io ar color os
原文地址:http://www.cnblogs.com/JohnTsai/p/4155117.html