[英]Android GridView not showing images
I have made a GridView
with an ImageAdapter
but it does not show the images inside.我制作了一个带有
GridView
的ImageAdapter
,但它没有显示里面的图像。 I tried to change numColumns
, columnWidth
and other attributes but it didn't work.我尝试更改
numColumns
、 columnWidth
和其他属性,但没有成功。 In Android Studio xml Design panel i can see my Gridview.在 Android Studio xml 设计面板中,我可以看到我的 Gridview。
This is my gridview
inside my xml
layout file:这是我的
gridview
布局文件中的xml
:
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linear_buttons"
android:layout_above="@+id/btnSearch"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="@null"
android:columnWidth="120dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
This is my Adapter:这是我的适配器:
public class CustomGridViewAdapter extends BaseAdapter {
private final Context mContext;
public CustomGridViewAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(R.dimen.grid_dimens_width, R.dimen.grid_dimens_height));
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setPadding(1, 5, 1, 1);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private final Integer[] mThumbIds = {
R.drawable.grid_agapis, R.drawable.grid_asteies, R.drawable.grid_auto,
R.drawable.grid_gamos, R.drawable.grid_goneis,
};
I set the adapter with the following code:我使用以下代码设置适配器:
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new CustomGridViewAdapter(this));
gridview.setOnItemClickListener(this);
Can you explain me where is the problem?你能解释一下问题出在哪里吗? Thank you.
谢谢你。
imageView.setLayoutParams(new GridView.LayoutParams(R.dimen.grid_dimens_width,
R.dimen.grid_dimens_height));
With this line of code you are trying to limit the size of the grabbed drawable to a fixed width & height that are equal to grid_dimens_width
& grid_dimens_height
respectively.使用这行代码,您试图将抓取的可绘制对象的大小限制为分别等于
grid_dimens_width
和grid_dimens_height
的固定宽度和高度。
But actually using R.dimen.foo
won't return the value of foo
, instead it returns the generated integer value of the resource itself which can be something like a big number (eg -21893103 or 33238590).. this will make you see nothing on the screen because the image is either:但实际上使用
R.dimen.foo
不会返回foo
的值,而是返回生成的资源本身的 integer 值,这可能是一个大数字(例如 -21893103 或 33238590)。屏幕上什么都没有,因为图像是:
What you need to do instead is to get the dimen
resource using getDimention()
and pass the resource id to it.您需要做的是使用
getDimention()
获取dimen
资源并将资源 ID 传递给它。
To apply that to your code:要将其应用于您的代码:
Replace:代替:
imageView.setLayoutParams(new GridView.LayoutParams(R.dimen.grid_dimens_width,
R.dimen.grid_dimens_height));
With:和:
imageView.setLayoutParams(new GridView.LayoutParams(
(int) mContext.getResources().getDimension(R.dimen.grid_dimens_width),
(int) mContext.getResources().getDimension(R.dimen.grid_dimens_height)));
Result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.