这是你的GridView添加到gridview.xml中
<GridView
android:id="@+id/clueGrid"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_below="@id/gameInstructions"
android:layout_centerHorizontal="true"
android:horizontalSpacing="20dp"
android:verticalSpacing="20dp"
android:paddingTop="10dp"
android:numColumns="2"
/>
将此添加到另一个xml文件中 --> grid_view_image.xml
<com.XXXX.view.SquareRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/grid_item_background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/clue_found"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true"
android:id="@+id/itemView"/>
</com.XXXX.view.SquareRelativeLayout>
并且那个 XML 使用 SquareRelativeLayout,将其添加到 Java 中。
public class SquareRelativeLayout extends RelativeLayout {
public SquareRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}
现在你需要创建你的适配器
public class ItemGridAdapter extends BaseAdapter{
List<Item> itemList;
public ItemGridAdapter(List<Item> itemList){
this.itemList = itemList;
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int position) {
return itemList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.grid_view_image, parent, false);
}
final ImageView itemView = (ImageView) convertView.findViewById(R.id.itemView);
itemView.setImageDrawable("YOUR DRAWABLE");
return convertView;
}
}
然后在使用gridview.xml的主类上
ItemGridAdapter itemGridAdapter = new ItemGridAdapter(itemList);
itemGrid.setAdapter(itemGridAdapter);
然后您可以在 ImageView 和布局上进行操作。希望这有所帮助!
我看到您在这里使用 Picasso,以下是我为适配器设置图像的答案:
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
itemView.setImageBitmap(bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso.with(convertView.getContext())
.load(imageUrl)
.into(target);
ScaleType.FIT_CENTER
? - Nevermore