GridViewDemo:网格示例
GridView标签内定义列宽,android:columnWidth="120dp"。
getView()内定义每一个ImageView的大小(115, 115),setScaleType设置图片显示的方式。
imageView.setLayoutParams(new GridView.LayoutParams(115, 115));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
package com.example.shad_fnst.gridviewdemo; import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
}
}
MainActivity.java
package com.example.shad_fnst.gridviewdemo; import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView; import java.util.Objects; /**
* Created by shad-fnst on 2015/08/04.
*/
public class ImageAdapter extends BaseAdapter {
private Context mContext; //keep all Images in array
public Integer[] mThumbIds = {
R.drawable.sample2, R.drawable.sample3,
R.drawable.sample4, R.drawable.sample5,
R.drawable.sample6, R.drawable.sample7,
R.drawable.sample0, R.drawable.sample1,
R.drawable.sample2, R.drawable.sample3,
R.drawable.sample4, R.drawable.sample5,
R.drawable.sample6, R.drawable.sample7,
R.drawable.sample0, R.drawable.sample1,
R.drawable.sample2, R.drawable.sample3,
R.drawable.sample4, R.drawable.sample5,
R.drawable.sample6, R.drawable.sample7,
}; //Constructor
public ImageAdapter(Context context){
mContext = context;
} public int getCount(){
return mThumbIds.length;
} public Object getItem(int position){
return null;
} public long getItemId(int position){
return 0;
} //create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent){
ImageView imageView;
if(convertView == null){
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(115, 115));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}else{
imageView =(ImageView) convertView;
} imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
ImageAdapter.java
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/gridview"
android:columnWidth="120dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center">
</GridView>
activity_main.xml