Android中的数据能够通过GridView类实现表格化展现,它也属于列表类型的控件,其继承关系如下:
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.widget.AdapterView<T extends android.widget.Adapter>
↳ android.widget.AbsListView
↳ android.widget.GridView
GridView控件应用案例效果如图6-10所示。
[img]http://dl.iteye.com/upload/attachment/515537/afc9acfa-529c-388f-88b7-ee2138e64fd5.png[/img]
图6-10 GridView控件使用效果图
程序代码请参考代码清单6-8:
【代码清单6-8】 chapter6_4/src/com/work/GridView_1.java
代码的关键是实现BaseAdapter适配器类——ImageAdapter,代码如下:
在convertView为null的时候实例化控件,imageView.setLayoutParams(new GridView.LayoutParams(60, 60)是设置一个单元格中图片大小是60×60像素。imageView.setPadding(8, 8, 8, 8)设定GridView的单元格与其中的图片之间上、下、左、右空白。
布局文件chapter6_4/res/layout/grid.xml:
android:verticalSpacing="10dp"是两行之间的边距,android:horizontalSpacing="10dp"是两列之间的边距。android:numColumns="auto_fit"GridView列数设置为自动,android:columnWidth="60dp"是每列的宽度,也就是项目的宽度。android:gravity="center"是居中。
出自《Android开发案例驱动教程》第六章
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.widget.AdapterView<T extends android.widget.Adapter>
↳ android.widget.AbsListView
↳ android.widget.GridView
GridView控件应用案例效果如图6-10所示。
[img]http://dl.iteye.com/upload/attachment/515537/afc9acfa-529c-388f-88b7-ee2138e64fd5.png[/img]
图6-10 GridView控件使用效果图
程序代码请参考代码清单6-8:
【代码清单6-8】 chapter6_4/src/com/work/GridView_1.java
public class GridView_1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
GridView g = (GridView) findViewById(R.id.myGrid);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(GridView_1.this, "图片ID:" + mThumbIds[position],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private Integer[] mThumbIds = { R.drawable.yellowimage,
R.drawable.redimage, R.drawable.icon, R.drawable.blueimage,
R.drawable.photo1962, R.drawable.photo189, R.drawable.photo1803,
R.drawable.photo1796, R.drawable.photo1732, R.drawable.photo1553,
R.drawable.photo1537, R.drawable.photo1503, R.drawable.photo1453,
R.drawable.photo1450, R.drawable.photo1447, R.drawable.photo1441,
R.drawable.photo1433, R.drawable.photo1432, R.drawable.photo1431,
R.drawable.photo1426, R.drawable.photo1425, R.drawable.photo1424,
R.drawable.photo1423, R.drawable.photo1422, R.drawable.photo142,
R.drawable.photo140, R.drawable.photo1220, R.drawable.photo1208,
R.drawable.photo1207, R.drawable.photo101209, };
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(60, 60));
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
}
代码的关键是实现BaseAdapter适配器类——ImageAdapter,代码如下:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(60, 60));
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
在convertView为null的时候实例化控件,imageView.setLayoutParams(new GridView.LayoutParams(60, 60)是设置一个单元格中图片大小是60×60像素。imageView.setPadding(8, 8, 8, 8)设定GridView的单元格与其中的图片之间上、下、左、右空白。
布局文件chapter6_4/res/layout/grid.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myGrid2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:gravity="center"
/>
android:verticalSpacing="10dp"是两行之间的边距,android:horizontalSpacing="10dp"是两列之间的边距。android:numColumns="auto_fit"GridView列数设置为自动,android:columnWidth="60dp"是每列的宽度,也就是项目的宽度。android:gravity="center"是居中。
出自《Android开发案例驱动教程》第六章