简体   繁体   中英

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

I've got a GridView full of images. 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.
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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM