2013-03-18 233 views
10

我有這樣的ListView一個的LinearLayout裏面:高度的ListView填滿整個屏幕,雖然設置爲WRAP_CONTENT

<?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="vertical" > 

    <TextView 
     .../> 

    <EditText /> 
     ... 
    </EditText> 


    <ListView 
     android:id="@+id/words_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</LinearLayout> 

名單由SimpleCursorAdapter編程填充:

adapter = new SimpleCursorAdapter(this, R.layout.list_row_search, mCursor, from, to); 

list_row_search有一個TableLayout與兩個TableRows:

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

    <View 
     android:layout_height="1dip" 
     android:background="#FF33B5E5" /> 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/list_lesson" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:padding="1dip" 
      android:textSize="12sp" /> 

     <TextView 
      android:id="@+id/list_name" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="5.25" 
      android:padding="1dip" 
      android:paddingRight="10dp" 
      android:textSize="18sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/list_flex" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="12.5" 
      android:padding="1dip" 
      android:paddingRight="10dp" /> 

     <TextView 
      android:id="@+id/list_rom" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="6.25" 
      android:padding="1dip" 
      android:paddingRight="10dp" /> 
    </TableRow> 

    <View 
     android:layout_height="0.1dip" 
     android:background="#404040" /> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/list_related" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="2" 
      android:paddingRight="10dp" 
      android:textStyle="italic" /> 

     <TextView 
      android:id="@+id/list_ant" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:paddingRight="10dp" /> 

     <TextView 
      android:id="@+id/list_engl" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:paddingRight="10dp" /> 
    </TableRow> 

    <View 
     android:layout_height="1dip" 
     android:background="#FF33B5E5" /> 

</TableLayout> 

現在,當列表被填充,L即使在數據庫中只找到一個元素,istView也會佔據洞屏幕。 eclipse中的HierarchyView顯示非常清晰,ListView是填充屏幕的那個。

你能告訴,問題在哪裏?謝謝你的時間!

回答

14

對於ListView的高度,您不應使用wrap_contentwrap_content的意思是「讓我儘可能大,以容納我所有的孩子。」當你認爲你的數據集可能非常大時,這聽起來應該是一個非常糟糕的主意。 由於您使用的是LinearLayout,請爲您的ListView layout_height="0dp"layout_weight="1"

可以讓ListView佔用屏幕的其餘部分。如果它只有一行,它將顯示一行,沒有什麼大不了的。除非你試圖在列表下面展示某些東西,但是我上面告訴你的東西應該可以實現這一點。

+1

感謝您的回答,但將高度從「wrap_content」更改爲0dp/weight = 1會導致相同的行爲:ListView會填滿整個屏幕。實際上,我試圖在列表下方顯示一些內容,但是僅顯示一個元素(當發現一個元素時)的整個列表視圖讓我把下面的列表放入... – 2013-03-18 15:50:25

+0

您仍然可以給它height = 0和weight = 1。在它之後添加另一個視圖,並且LinearLayout將爲該項目分配空間並將剩餘部分給予ListView。如果你試圖讓一個固定在列表底部的項目,但滾動列表的內容,你想看看'ListView.addFooterView()' – Karakuri 2013-03-19 14:36:36

0

如果上述不起作用,您可以使用RelativeLayout而不是線性,並將ListView放在EditText的下面。