2010-10-22 79 views
0

我正在嘗試在一個表內部使一個ListView消耗掉所有可用的垂直空間減去EditText控件所需的空間。如何讓一個Android ListView在TableLayout中消耗剩餘空間

我已經設置每個屬性我能想到的在這裏,使其工作:

<?xml version="1.0" encoding="utf-8"?> 

<TableRow android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FF0000"> 
    <ScrollView android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <ListView android:id="@+id/conversation" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:drawSelectorOnTop="false"/> 
    </ScrollView> 
</TableRow> 

<TableRow android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#00FF00"> 
    <EditText android:id="@+id/messagetext" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:inputType="text|textAutoCorrect|textMultiLine|textImeMultiLine" 
     android:imeOptions="actionDone"/> 
</TableRow> 

我必須失去了一些東西,因爲結果是完全填充水平的,但ListView和EditText看起來好像其屬性是wrap_content一樣。

回答

0

我似乎在頂部的TableRow中缺少屬性android:layout_weight。顯然,超過2的任何東西都會消耗垂直房地產的其餘部分。任何人都可以解釋爲什麼TableRows的特殊處理?

+0

你的layout_weight不需要4的值。如果它是歸因於它的唯一元素,則可以將其賦值爲1. – 2010-10-22 17:37:28

0

請勿在ScrollView之內使用ListView,因爲ListView管理它自己的垂直滾動。這樣做會殺死所有優化的完成ListView

而且不要去你自己的帖子。編輯您的初始問題或添加評論。

+5

如果解決了問題,回答自己的問題完全有效。 – 2010-10-22 17:35:19

1

你有使用TableLayout的特殊原因嗎?我對使用它們還不是很熟悉,但是你想要完成的事情很簡單,就是使用RelativeLayout。此外,您不需要將ListView放置在ScrollView中,ListView自行處理滾動。這裏是你如何能做到這一點使用RelativeLayout的一個例子:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <EditText 
     android:id="@+id/message_text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#00FF00" 
     android:inputType="text|textAutoCorrect|textMultiLine|textImeMultiLine" 
     android:imeOptions="actionDone" 
     android:alignParentBottom="true" 
     /> 
    <ListView 
     android:id="@+id/conversation" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#FF0000" 
     android:layout_above="@id/message_text" 
     /> 
</RelativeLayout> 

這樣,你先定義的EditText要佔用一定的空間(WRAP_CONTENT,在這種情況下)。然後,您定義ListView以用fill_parent填充剩餘空間。添加android:layout_above =「@ id/message_text」將ListView的下邊緣與EditText視圖的上邊緣對齊。