我目前正在开发一个 android 应用程序,该应用程序需要显示一个书架,其中每行最多包含 3 个图像。我为此使用了gridview。但是我被困在一个地方,我无法在gridview中更改整行的背景。谁能告诉我该怎么做?
用于gridview的适配器
private class SampleGridAdapter extends BaseAdapter{
private Context context;
private int[] images = {"R.drawable.img1","R.drawable.img2","R.drawable.img3","R.drawable.img4","R.drawable.img5","R.drawable.img6","R.drawable.img7"};
public SampleGridAdapter(Context context) {
super();
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View v = null;
ImageView coverImageView;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.bookgrid, null);
coverImageView = (ImageView) v.findViewById(R.id.coverImageView);
coverImageView.setImageResource(images[position]);
coverImageView.setScaleType(ImageView.ScaleType.FIT_XY);
return v;
}
}
bookgrid.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shelfimage" >
<ImageView
android:id="@+id/coverImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingBottom="0dp"
android:paddingLeft="25dp"
android:paddingTop="6dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
我在 gridview 声明中将列数设置为 3。在这里,我将 xml 文件中的背景设置为 android:background="@drawable/shelfimage"。但它是为每个项目单独设置背景......我想为每一行设置一个图像作为背景(有3个项目)......