1

我尝试在 gridview.bt 中使用我无法获得的整数值图像。

这是我的价值观/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));
}

}

公共类 ImageAdapter 扩展 BaseAdapter { 私有上下文 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;
}
4

1 回答 1

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);
于 2012-05-07T10:36:01.910 回答