這是如何通過編程創建的GridView,
GridView grid = new GridView(this);
grid.setId(ViewIdentification.getId());
grid.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
grid.setBackgroundColor(Color.WHITE);
grid.setNumColumns(3);
grid.setColumnWidth(GridView.AUTO_FIT);
grid.setVerticalSpacing(5);
grid.setHorizontalSpacing(5);
grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
添加上述觀點來佈局。在這裏你可以得到顯示器的高度和寬度。
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay()
.getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;
這裏是適配器類:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Bitmap[]mis_fotos;
public ImageAdapter(Context c) {
mContext = c; }
public int getCount() {
return mis_fotos.length;
}
public Object getItem(int position) {
return position; }
public long getItemId(int position) {
return 0; }
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(width/3, height/3));
imageView.setScaleType(ImageView.setScaleType(ScaleType.FIT_XY));
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(mis_fotos[position]);
return imageView;
}
}
然後,它高達你,如果你是通過動態的URL,相應地改變你的適配器。 將你的適配器設置爲你的gridview。 如果您有任何問題,請告知我。
其中我必須寫上述2個部分? –
在'OnCreate'中創建一個gridview並獲取寬度和高度。之後創建適配器類 –
您可以請發佈完整的代碼? –