当我在屏幕4.3上运行我的应用程序时,它工作正常,但是当我在3.14屏幕上运行时,图像无法正确地显示在另一个上面。
有没有办法使用百分比来显示图像,这取决于不同的屏幕尺寸?
以下是ImageView代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(200,200 ));
return imageView;
}
答案 0 :(得分:3)
我找到了答案的解决方案 它接近@dmon回答
我在Values文件夹
中创建了dimension.xml<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="width">150dp</dimen>
<dimen name="height">150dp</dimen>
</resources>
和imageadapter.java
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(
(int) mContext.getResources().getDimension(R.dimen.width),
(int) mContext.getResources().getDimension(R.dimen.height)));
imageView.setPadding(5, 5, 5, 5);
return imageView;
}
谢谢大家都试图帮忙
最好的问候
答案 1 :(得分:1)
//有与屏幕英寸天气有关的4'或3.2
关于屏幕分辨率的问题,无论是ldpi,mdpi,hdpi还是xhdpi。
final int scaleImage=200;
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
final int imgSize = (int) (scaleImage * scale + 0.5f);
// Use imgSize as a distance in pixels...
this 200 will give you a pixel as
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(imgSize,imgSize ));
return imageView;
}
答案 2 :(得分:0)
尝试使用此代码获取屏幕尺寸
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.heightPixels;
metrics.widthPixels;
或者
getContext().getResources().getDisplayMetrics().widthPixels
getContext().getResources().getDisplayMetrics().heightPixels
答案 3 :(得分:0)
创建dimens.xml文件:
<resources>
<dimen name="gridview">200dp</dimen>
</resources>
然后获取该资源:
int size = mContext.getResources().getDimensionPixelSize(R.dimen.gridview);
imageView.setLayoutParams(new GridView.LayoutParams(size, size));
您可以在the docs中阅读维度资源。