[英]How to make an ImageView's height be the image's height? Android
I've got a GridView full of images. 我有一个GridView充满了图像。 Here's my custom adapter:
这是我的自定义适配器:
public class ImageAdapter extends ArrayAdapter<String> {
private final String LOG_TAG = ImageAdapter.class.getSimpleName();
public ImageAdapter(Activity context, List<String> urls){
super(context, 0, urls);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
String url = getItem(position);
View rootView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
ImageView img = (ImageView) rootView.findViewById(R.id.grid_view_item);
img.setLayoutParams(
new GridView.LayoutParams(
GridView.AUTO_FIT,
500));
img.setScaleType(ImageView.ScaleType.FIT_XY);
Picasso.with(getContext())
.load(url)
.into(img);
return rootView;
}
}
You can see that I set the height to be 500. But I don't want this. 你可以看到我将高度设置为500.但我不想要这个。
I want the height to be the actual height of the image, just like the width. 我希望高度是图像的实际高度,就像宽度一样。
How can I do that? 我怎样才能做到这一点?
Use adjustViewBounds
property to force the height to the correct dimension. 使用
adjustViewBounds
属性将高度强制为正确的尺寸。
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.