在我的应用程序中,我在列表的每一行中都有一个 ListView 和一个 ImageView。这个 ImageView 填充了来自网络的图像。当我下载第一张图片时,我计算了位图的高度,因为我必须根据这个位图的高度来设置列表项的高度。我没有占位符,所以我选择这种方法来避免在填充图像时扩展每个列表项。
我面临的问题是当我尝试调整 ImageView 的大小时,实际上调整整个 View 组的大小,在本例中为 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);
}
最佳答案
我想知道为什么您选择使用 LinearLayout 来创建列表而不是使用 ListView ( http://developer.android.com/reference/android/widget/ListView.html ) 我以前使用过它们并且它们在创建您不确定的列表时更加可靠列表中将包含多少项。使用 ListView 时,您还可以自定义每行的确切大小,这样您就不必为每个元素动态更改行大小。
此外,从您的描述来看,您似乎想将图像放置在 Textview 的一侧。如果是这样的话,我之前自己偶然发现了这一点,你实际上可以在不使用 ImageView 的情况下做到这一点。 有关更多详细信息,请参见以下链接: http://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableLeft
关于Android 调整 ImageView 大小导致 View 容器调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30111162/