我正在开发一个 Android 应用程序,但我的 GUI (The GridLayout) 有问题。
我想知道是否可以选择包含视图的列,因为我必须在循环中填充前两列,然后再填充其他列。
我正在开发一个 Android 应用程序,但我的 GUI (The GridLayout) 有问题。
我想知道是否可以选择包含视图的列,因为我必须在循环中填充前两列,然后再填充其他列。
上面的示例是一个显示了一些图片(mThumbIds)的 gridView。
通过 xml 创建一个 gridView。
然后像这样在您的代码中获取它:
GridView gv = (GridView) findViewById(R.id.gridView1);
然后创建一个图像 Adapter,并将其附加到您的GridView:
imageAdapter = new ImageAdapter(this);
gv.setAdapter(imageAdapter);
这是您的 ImageAdapter 的代码:
为网格中的每个项目执行getView 方法。
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 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) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}