2012-04-01 75 views
0

我有以下列表視圖:如何讓列表項目包裝內容?

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:drawSelectorOnTop="false" /> 

這裏的問題是,如果列表項中包含大量的文字,不是所有的顯示。有沒有辦法讓每個列表項目足夠高以顯示其所有內容?

+0

如何將listem添加到列表以及用於每行的xml – Ishu 2012-04-01 11:08:08

+0

@DilSe我在Java代碼中動態添加它,並使用'listItems.add(item)' – user1301428 2012-04-01 12:05:08

回答

0

我找到的最簡單的解決方案是專門爲列表行創建一個XML文件。這樣,列表項會自動調整其高度以顯示所有文本。該XML文件是很簡單的:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
</TextView> 

更改android:layout_heightwrap_content不作任何不同,它工作在兩種方式。

現在我只需告訴適配器使用此文件而不是默認的佈局。

2

您必須創建行xml並自定義高度,因爲您需要取決於您在每行上的內容。 如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/Layout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
     > 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="fill_parent" 
     android:layout_height="90dp" 
     android:text="TextView" 
     android:textSize="15dp" /> 


</LinearLayout> 
+0

看起來應該設置您的LinearLayout的layout_height到「wrap_content」。 – 2012-04-01 11:21:46

+0

已經檢查過它不會影響LinearLayout它自身的變化,但是改變TextView的android:layout_height的技巧,也可以自定義textSize取決於你需要的什麼 – 2012-04-01 11:44:18

+0

但是我的佈局沒有任何TextView,我只有一個ListView ... – user1301428 2012-04-01 12:02:32

0

你應該通過自身設置的列表項的高度,而不是ListView

+0

正是我的問題:)我該怎麼做? – user1301428 2012-04-01 12:03:02

+0

這篇文章並沒有提到BaseAdapter,我可能在這裏丟失了一些東西:D – user1301428 2012-04-01 12:20:16

+0

@ user1301428,哦,對不起。這是正確的鏈接)http://thinkandroid.wordpress.com/2010/01/13/custom-baseadapters/ – 2012-04-01 12:26:41

0

如果您正在使用列表視圖來顯示列表項,我建議你使用CustomAdapter這樣做,

這裏是LINK爲示例實施

+0

這會解決我的問題嗎?我在問你,因爲我只知道兩者在表現上的差異。 – user1301428 2012-04-01 12:10:25