简体   繁体   English

如何使ImageView的高度成为图像的高度? Android的

[英]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.

相关问题 将 ImageView 高度设置为宽度的 50% - Set ImageView Height 50% Of That's Width 当Android中imageview具有width和height值时,如何将textview和imageview合并为图像复合? - How to combine textview and imageview into image compound when imageview has width and height values in android? Android:如何在不使用 JavaX 的 ImageIO 的情况下获取图像的宽度和高度? - Android: How to get width and height of an image without using JavaX's ImageIO? 如何以编程方式调整 android 中选项卡的高度和加粗文本? - How to adjust the height and bold the text of the tab's in android programmatically? 在Android中,如何设置ListView项的高度和宽度? - In Android, how can I set a ListView item's height and width? Android:ConstraintLayout 高度大于模拟器屏幕高度 - Android: ConstraintLayout height is bigger than emulator screen's height 如何使imageView在折叠工具栏达到一定高度时熄灭 - How to make imageView go out as collapsingtoolbar reach at certain height Android:setBackground 方法更改视图的高度 - Android : setBackground method changes View's height 如何使用Android应用程序中的按钮增加/减少imageView的宽度和高度? - how to increase/decrease imageView width and height with buttons in android app? 如何以编程方式设置imageView的高度? - How to set height of the imageView programatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM