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

How to make an ImageView's height be the image's height? Android

我有一个充满图像的 GridView。这是我的自定义适配器:

    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;
    }
}


你可以看到我把高度设置为500。但是我不想这样。
我希望高度是图像的实际高度,就像宽度一样。
我该怎么做?

使用 adjustViewBounds 属性 强制高度为正确尺寸。

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true" />