XML属性 | 方法 | 说明 |
android:columnWidth | setColumnWidth(int columnWidth) | 设置列的宽度,好像没效果 |
android:gravity | setGravity(int gravity) | 对齐方式 |
android:horizontalSpacing | setHorizontalSpacing(int horizontalSpacing) | 个元素的水平间距 |
android:verticalSpacing | setVerticalSpacing(int verticalSpacing) | 垂直间距 |
android:numColumns | setNumColumns(int numColumns) | 一行显示几列 |
android:stretchMode | setStretchMode(int stretchMode) | 拉伸模式 |
和ListView差不多,直接上例子
没做优化处理,所以很卡,只压缩了下图片
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <GridView android:id="@+id/gridView1" android:layout_width="match_parent" android:layout_height="300dp" android:numColumns="4" > </GridView> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:scaleType="fitStart" android:layout_height="603dp" /> </LinearLayout>
item1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <ImageView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="100dp" android:scaleType="fitXY" /> </LinearLayout>
MainActivity.java
public class MainActivity extends Activity { //pic图片合集 List<Map<String,Object>> images; ImageView imageView1; private GridView gv; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取图片,图片太多直接用反射读取图片ID getImages(); imageView1=(ImageView)findViewById(R.id.imageView1); //配置adapter // SimpleAdapter sa=new SimpleAdapter(getApplicationContext(), images, // R.layout.item1, new String[]{"image","name"}, new int[]{R.id.imageView1,R.id.textView1}); gv=(GridView)findViewById(R.id.gridView1); MyAdapter sa=new MyAdapter(this); gv.setAdapter(sa); //点击grid的图片后,显示到下面的大的imageview里 gv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Integer id1=Integer.parseInt(images.get(position).get("image")+""); imageView1.setImageResource(id1); } }); //Integer id1=Integer.parseInt(images.get(1).get("image")+""); //imageView1.setImageResource(id1); } private void getImages(){ images=new ArrayList<Map<String,Object>>(); // 反射获得所有的字段 Field[] filed = R.drawable.class.getFields(); // 遍历 for (Field field : filed) { Log.v("", field.getName()); // pic开头 if (field.getName().startsWith("pic")) { try { Map<String,Object> map=new HashMap<String,Object>(); map.put("image", field.getInt(field.getName())); map.put("name", field.getName()); images.add(map); } catch (IllegalAccessException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } } class MyAdapter extends BaseAdapter{ Context context; public MyAdapter(Context context){ this.context=context; } @Override public int getCount() { // TODO 自动生成的方法存根 return images.size(); } @Override public Object getItem(int position) { // TODO 自动生成的方法存根 return null; } @Override public long getItemId(int position) { // TODO 自动生成的方法存根 return 0; } @Override public View getView(int position, View view, ViewGroup parent) { if(view==null){ view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.item1,null); } Integer id1=Integer.parseInt(images.get(position).get("image")+""); Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),id1); bitmap=zoomImg(bitmap,bitmap.getWidth()/10,bitmap.getHeight()/10); ImageView imageView1=(ImageView)view.findViewById(R.id.imageView1); imageView1.setImageBitmap(bitmap); //imageView1.setImageResource(id1); TextView tv=(TextView)view.findViewById(R.id.textView1); tv.setText(images.get(position).get("name")+""); return view; } public Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight){ // 获得图片的宽高 int width = bm.getWidth(); int height = bm.getHeight(); // 计算缩放比例 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 取得想要缩放的matrix参数 Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); // 得到新的图片 Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true); return newbm; } } }