2013-08-28 60 views
1

工作:安卓的ListView layout_height爲wrap_content它只是包裹在ListView第一項:WRAP_CONTENT不會在列表視圖

 ... 
    <ListView android:id="@+id/points_add_list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/points_add_progress"/> 

    <ImageView android:id="@+id/points_add_progress2" 
     android:layout_width="fill_parent" 
     android:layout_height="2dp" 
     android:layout_below="@id/points_add_list" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:src="@android:color/darker_gray"/> 
</RelativeLayout> 
</ScrollView> 

enter image description here

如果我使用例如300dp而不是wrap_content,其他一些項目是可見的。

我的列表視圖適配器自定義視圖佈局XML:

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

    <TextView android:id="@+id/points_add_row_title" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     style="@style/PlaneText"/> 
    <RadioButton android:id="@+id/points_add_row_radio" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 
+0

[ListView的高度填充整個屏幕,儘管設置爲wrap \ _content]可能的重複(http://stackoverflow.com/questions/15479915/height-of-listview-fills-the-whole-screen-although -set-as-wrap-content) – CommonsWare

+0

我發現如果我刪除ScrollView,列表視圖顯示所有項目。爲什麼? – user2136334

回答

2

我想補充一點意見,但我的名聲是不夠的。我的意見可能是錯誤的。

Karakuri在CommonsWare的鏈接上的答案是完全正確的答案我認爲。 和這個谷歌的傢伙說,爲什麼我們不應該在ListView上使用wrap_content。 http://www.youtube.com/watch?v=wDBM6wVEO70&t=40m40s

而關於你的評論,我的意見是,
滾動型的高度可能與滾動WRAP_CONTENT垂直。 如果那麼ScrollView的高度將是適配器的高度或ImageView的高度。 Bcuz,ScrollView並不關心應該顯示多少個ListItem。它只檢查其內部存在多少視圖或組視圖。

而且我也建議你不要在ScrollView(或相反)中使用ListView。 因爲,這兩個視圖都必須檢查觸摸移動事件以滾動其視圖。如果混合使用,android無法決定應該滾動哪個視圖,如果兩個視圖都具有相同的滾動方式。

+0

非常感謝,我不應該同時使用滾動視圖和列表視圖,但我的ScrollView的高度是mach_parent而不是wrap_content。 – user2136334