android listview与图像项目按比例调整图像高度

时间:2017-04-11 10:48:44

标签: android listview picasso

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

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

我的问题是所有行都修复了imageview高度,我需要将imageview高度设置为与在线图像相同的图像高度。所以它可以采取其确切的高度

1 个答案:

答案 0 :(得分:1)

来自this教程

  

fit()测量目标ImageView的尺寸,并在内部使用resize()将图像大小缩小为ImageView的尺寸。 fit()有两点需要了解。首先,调用fit()可以延迟图像请求,因为Picasso需要等到可以测量ImageView的大小。其次,您只能将fit()与ImageView一起用作目标。

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

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

相关问题