2012-03-01 84 views
6

我看到一個奇怪的行爲與我的ListView,我似乎無法解決。Android的ListView行的動態高度

我有一個ListView,填充了一些數據。每行包含一個圖像和三個文本標籤。這裏是我的XML爲行(我誇大它的適配器的getView法):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="0dip" 
    android:paddingBottom="0dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <ImageView android:id="@+id/Icon" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:padding="2dp" 
     android:scaleType="fitCenter" 
     android:contentDescription="@string/app_icon"/> 

    <TextView android:id="@+id/TxtName" 
     android:scrollHorizontally="false" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:textColor="@android:color/black" 
     android:background="@color/list_bg1" 
     android:layout_weight="2" 
     android:padding="2dp"/> 

    <TextView android:id="@+id/TxtPackage" 
     android:scrollHorizontally="false" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="2" 
     android:textColor="@android:color/black" 
     android:background="@color/list_bg2" 
     android:padding="2dp"/> 

    <TextView android:id="@+id/TxtSource" 
     android:scrollHorizontally="false" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="2" 
     android:background="@color/list_bg3" 
     android:textColor="@android:color/black" 
     android:padding="2dp"/> 
</LinearLayout> 

現在,無論進入文本字段可以是任何lenght的(從幾個字符以80-100字)並且我希望標籤只是包裝並且行的高度要展開以適應包裝標籤的高度。

然而,標籤不換行,但該行的高度不會擴大,因此標籤的底部被切斷。這裏是我的getView方法:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    SetInfo app = (SetInfo)getItem(position); 
    if(app == null) 
     return null; 

    LinearLayout row = (LinearLayout) (convertView == null 
       ? LayoutInflater.from(context).inflate(R.layout.listitem, parent, false) 
       : convertView); 
    ((TextView)row.findViewById(R.id.TxtName)).setText(app.getName()); 
    ((TextView)row.findViewById(R.id.TxtPackage)).setText(app.getPackage()); 
    ((TextView)row.findViewById(R.id.TxtSource)).setText(app.getSource()); 
    if(app.getIcon() != null) 
     ((ImageView)row.findViewById(R.id.Icon)).setImageDrawable(app.getIcon()); 
    return row; 
} 

怎樣纔可以有行是不同高度,自動調整,以適合所有內容?

+0

更好的辦法採用Android:MAXLINES = 1的TextView行其防止更多然後一個文本行 – 2012-03-01 14:50:20

+0

的佈局,你試過adujusting安卓layout_weight 1或東西嗎? – 2012-03-01 14:51:40

+0

@Samir:我想要的是完全相反:標籤必須包裝,因爲我需要所有文本都可見。 – 2012-03-01 15:06:25

回答

8

試着改變你的TextViews有機器人:layout_height = 「WRAP_CONTENT」。

+0

這幾乎是我想要的。也就是說,行的高度現在擴展到允許包裝標籤,但是我失去了通過將不同背景顏色設置爲不同標籤來正確着色列的能力。現在,顏色只能延伸到標籤包裝的最下方,如果其他標籤包裝更多線條,我會在我的彩色標籤下獲得白色默認背景。任何想法如何解決這個問題? – 2012-03-07 14:17:32

+0

我會接受這個答案,因爲這最終是我不得不做的 - 然後用一大堆其他痛苦的處理來按照我想要的方式獲得佈局。 – 2012-03-16 09:38:50

4

我有SAM問題和@ MAEBE的回答並沒有爲我工作。爲了使它起作用,我必須在適配器的getView中的TextView上調用requestLayout()。

所以,你的情況,你就必須調用requestLayout()每個TextView的:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    // Inflate your view etc. 

    row.findViewById(R.id.TxtName).requestLayout(); 
    row.findViewById(R.id.TxtPackage).requestLayout(); 
    row.findViewById(R.id.TxtSource).requestLayout(); 
    return row; 
} 

我試過各種技巧,這僅僅是我的情況下工作的人。

+0

此黑客+1。創造了我的一天。非常感謝@muscardinus – niranjan94 2014-07-15 10:55:48