android 带有图像项的列表视图按比例调整图像高度

android listview with image item resize image height proportionaly

我有一个包含此图片项的列表视图:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/llItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@color/white"

android:layoutDirection="rtl"
android:orientation="horizontal">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="5dp"
    android:background="@color/white"
    app:cardElevation="4dp"
    app:cardUseCompatPadding="true">

    <ImageView
        android:id="@+id/pollimg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"


        />
</android.support.v7.widget.CardView>

我需要将 getview 方法中的图像视图加载到数组适配器中,如下所示:

                 @Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
              .... 
                etc
              .....
        try {
            Picasso.with(context)
                    .load(allpolls.get(position).getMediaUrl()).fit()
                    .placeholder(R.mipmap.icon).into(holder.img);
        } catch (Exception e) {
            holder.img.setVisibility(View.GONE);
        }
         }

我的问题是所有行的图像视图高度都是固定的,我需要将图像视图高度设置为与在线图像相同的图像高度。所以它可以达到它的确切高度

来自this教程

fit() is measuring the dimensions of the target ImageView and internally uses resize() to reduce the image size to the dimensions of the ImageView. There are two things to know about fit(). First, calling fit() can delay the image request since Picasso will need to wait until the size of the ImageView can be measured. Second, you only can use fit() with an ImageView as the target.

这意味着,如果您使用 fit(),图像将被调整大小。删除它。

有关详细信息,如果您想保持图片的原始宽高比,您应该考虑将高度和宽度都设置为 wrap_content