簡體   English   中英

如何使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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM