标签:
GridViewDemo:网格示例
GridView标签内定义列宽,android:columnWidth="120dp"。
getView()内定义每一个ImageView的大小(115, 115),setScaleType设置图片显示的方式。
imageView.setLayoutParams(new GridView.LayoutParams(115, 115));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
1 package com.example.shad_fnst.gridviewdemo; 2 3 import android.app.Activity; 4 import android.support.v7.app.ActionBarActivity; 5 import android.os.Bundle; 6 import android.view.Menu; 7 import android.view.MenuItem; 8 import android.widget.GridView; 9 10 11 public class MainActivity extends Activity { 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 18 GridView gridView = (GridView) findViewById(R.id.gridview); 19 gridView.setAdapter(new ImageAdapter(this)); 20 } 21 22 @Override 23 public boolean onCreateOptionsMenu(Menu menu) { 24 // Inflate the menu; this adds items to the action bar if it is present. 25 getMenuInflater().inflate(R.menu.menu_main, menu); 26 return true; 27 } 28 29 @Override 30 public boolean onOptionsItemSelected(MenuItem item) { 31 // Handle action bar item clicks here. The action bar will 32 // automatically handle clicks on the Home/Up button, so long 33 // as you specify a parent activity in AndroidManifest.xml. 34 int id = item.getItemId(); 35 36 //noinspection SimplifiableIfStatement 37 if (id == R.id.action_settings) { 38 return true; 39 } 40 41 return super.onOptionsItemSelected(item); 42 } 43 }
1 package com.example.shad_fnst.gridviewdemo; 2 3 import android.content.Context; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.BaseAdapter; 7 import android.widget.GridView; 8 import android.widget.ImageView; 9 10 import java.util.Objects; 11 12 /** 13 * Created by shad-fnst on 2015/08/04. 14 */ 15 public class ImageAdapter extends BaseAdapter { 16 private Context mContext; 17 18 //keep all Images in array 19 public Integer[] mThumbIds = { 20 R.drawable.sample2, R.drawable.sample3, 21 R.drawable.sample4, R.drawable.sample5, 22 R.drawable.sample6, R.drawable.sample7, 23 R.drawable.sample0, R.drawable.sample1, 24 R.drawable.sample2, R.drawable.sample3, 25 R.drawable.sample4, R.drawable.sample5, 26 R.drawable.sample6, R.drawable.sample7, 27 R.drawable.sample0, R.drawable.sample1, 28 R.drawable.sample2, R.drawable.sample3, 29 R.drawable.sample4, R.drawable.sample5, 30 R.drawable.sample6, R.drawable.sample7, 31 }; 32 33 //Constructor 34 public ImageAdapter(Context context){ 35 mContext = context; 36 } 37 38 public int getCount(){ 39 return mThumbIds.length; 40 } 41 42 public Object getItem(int position){ 43 return null; 44 } 45 46 public long getItemId(int position){ 47 return 0; 48 } 49 50 //create a new ImageView for each item referenced by the Adapter 51 public View getView(int position, View convertView, ViewGroup parent){ 52 ImageView imageView; 53 if(convertView == null){ 54 imageView = new ImageView(mContext); 55 imageView.setLayoutParams(new GridView.LayoutParams(115, 115)); 56 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 57 imageView.setPadding(8, 8, 8, 8); 58 }else{ 59 imageView =(ImageView) convertView; 60 } 61 62 imageView.setImageResource(mThumbIds[position]); 63 return imageView; 64 } 65 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <GridView xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingLeft="@dimen/activity_horizontal_margin" 6 android:paddingRight="@dimen/activity_horizontal_margin" 7 android:paddingTop="@dimen/activity_vertical_margin" 8 android:paddingBottom="@dimen/activity_vertical_margin" 9 android:id="@+id/gridview" 10 android:columnWidth="120dp" 11 android:numColumns="auto_fit" 12 android:verticalSpacing="10dp" 13 android:horizontalSpacing="10dp" 14 android:stretchMode="columnWidth" 15 android:gravity="center"> 16 </GridView>
标签:
原文地址:http://www.cnblogs.com/hello-sandy/p/4701906.html