在我的应用程序中,我有一个ListView与列表中的每一行ImageView。这个ImageView由来自网络的图像组成。当我下载第一张图片时,我计算了位图的高度,因为我必须根据此位图的高度来设置列表项的高度。我没有占位符,所以我选择这种方法来避免在图像填充时扩展每个列表项。Android的调整大小的ImageView导致调整大小的视图容器
我面临的问题是当我尝试调整ImageView的大小时,实际上调整了整个视图组的大小,在这种情况下是LinearLayout。
下面是一些代码:
列表项目布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingTop="8dp"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:textSize="30sp" />
<ImageView
android:id="@+id/photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
</LinearLayout>
方法做了调整大小:
private void resizeImageContainer(ViewHolder viewHolder, int height) {
LinearLayout.LayoutParams imageLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, height);
viewHolder.photo.setLayoutParams(imageLayoutParams);
}
也许你应该尝试chagne它的大小是这样的:vievHolder.photo.getLayoutParams()高度=高度; vievHolder.photo.requestLayout(),只是为了确保这个方法在这里不是问题 –
尝试在另一个LinearLayout中包装imageView。适配器在保持没有嵌套LInearLayouts的父类的正确参数方面做得不错。 –