在gallery中,如何为android中的每个图像提供不同的背景
您好,我在这里做一个应用程序,我需要水平显示图像。所以使用gallery我显示图像。但我需要gallery中每个图像的不同背景。我不知道怎么做。 我给了图库背景,但整个图库只有一个相同的背景。但我需要每个图像有不同的背景。任何人都可以帮助我。我使用下面的代码创建树在gallery中,如何为android中的每个图像提供不同的背景,android,android-emulator,Android,Android Emulator,您好,我在这里做一个应用程序,我需要水平显示图像。所以使用gallery我显示图像。但我需要gallery中每个图像的不同背景。我不知道怎么做。 我给了图库背景,但整个图库只有一个相同的背景。但我需要每个图像有不同的背景。任何人都可以帮助我。我使用下面的代码创建树 example .class: public class example extends Activity { /** Called when the activity is first created.
example .class:
public class example extends Activity {
/** Called when the activity is first created. */
private Gallery gallery;
private ImageView imgView;
private Integer[] Imgid = {
R.drawable.popup2, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return Imgid.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
}
我认为
GalItemBg
变量是在适配器类的构造函数中初始化的,在调用getView
方法时它没有改变,因此您得到了相同的背景
试一试
查看这个ImageAdapter示例。您应该有一些对drawables中图像的引用的sorf列表,并使用imageView.setImageResource(mimageId[position])在适配器的getView方法中设置它们
您必须使用适配器的getView方法来设置不同Imageview的背景图像 //存储背景图像 整数[]backGroundImage={R.drawable.background1,R.drawable.background2,R.drawable.background3} 公共视图getView(int位置、视图转换视图、视图组父视图){ //后退地面图像 setBackgroundResource(backGroundImage[count]); }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(Imgid[position]);
return imgView;
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
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
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = attr.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
attr.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
ImageView imageView = new ImageView(cont);
-----
-----
----