如果可能对您的 xml 文件有帮助,您可以尝试此代码
<GridView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/gallerylistactivity_gallery"
android:columnWidth="90dp" android:numColumns="4"
android:verticalSpacing="5dp" android:horizontalSpacing="10dp"
android:gravity="center" android:stretchMode="spacingWidth"/>
.java 文件
公共类 GalleryListAdapter 扩展 BaseAdapter{
private Context mContext;
private Integer[] mImageIds = { R.drawable.gallery_01,
R.drawable.gallery_02, R.drawable.gallery_03,
R.drawable.gallery_04, R.drawable.gallery_05,
R.drawable.gallery_06, R.drawable.gallery_07,
R.drawable.gallery_08, R.drawable.gallery_10,
R.drawable.gallery_09 };
public GalleryListAdapter(Context c) {
this.mContext = c;
}
@Override
public int getCount() {
return mImageIds.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(3, 3, 3, 3);
return imageView;
}