我正在尝试使用gridview.bt给出整数值图像我无法得到。
这是我的values / string.xml
<integer-array name="anim">
<item >@drawable/goat1</item>
<item >@drawable/lamb1</item>
<item>@drawable/bear</item>
<item>@drawable/lion</item>
</integer-array>
这是我的java代码:
公共类GridViewActivity扩展了Activity {
**int[] anim=getResources().getIntArray(R.array.anim);**
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
}
}
public class ImageAdapter扩展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;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
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(anim[position]);
return imageView;
}
答案 0 :(得分:2)
use TypedArray,
TypedArray icons;
Resources res = getResources();
icons = res.obtainTypedArray(R.array.img);
然后在图像适配器中,而不是imageView.setImageResource(anim[position]);
:
Drawable drawable = icons.getDrawable(position);
imageview.setImageDrawable(drawable);