2013-03-18 68 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的下面。