2015-05-07 118 views
1

在我的應用程序中,我有一個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); 
} 
+0

也許你應該嘗試chagne它的大小是這樣的:vievHolder.photo.getLayoutParams()高度=高度; vievHolder.photo.requestLayout(),只是爲了確保這個方法在這裏不是問題 –

+0

嘗試在另一個LinearLayout中包裝imageView。適配器在保持沒有嵌套LInearLayouts的父類的正確參數方面做得不錯。 –

回答

1

我想知道爲什麼你選擇使用LinearLayout創建一個列表而不是使用ListView(http://developer.android.com/reference/android/widget/ListView.html)我有你sed他們之前,他們是更可靠的創建列表,你不知道列表中有多少項目。使用ListView時,您還可以自定義每行的確切大小,以便您不必爲每個元素動態更改行大小。

此外,從您的描述來看,似乎您想要將圖像放置在Textview的一側。如果是這樣的話,我以前就偶然發現了這個,你可以在不使用ImageView的情況下做到這一點。 請參閱以下鏈接瞭解詳情: http://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableLeft