2011-09-13 51 views
0

我搜索了類似的問題,但沒有得到正確的答案,因此將其作爲新問題發佈。LinearLayout中的TextView和ListView

可以有一個LinearLayout有兩個TextViews和一個列表視圖?我知道只有當類擴展ListActivity時,listview才能用來顯示數組元素。我的應用程序中的類擴展了Activity並使用了listView。

我試圖建立一個基於IBM tutorial的RSS閱讀器。在運行該項目時,我在兩個文本視圖中都獲得了分析文本,但不顯示ListView。如果需要,我可以發佈代碼和Logcat。

+0

發佈代碼,這會讓事情變得更好 –

回答

1

您可以在任何活動中使用ListView,使用ListActivity只會讓您的生活更輕鬆(通常)。有關詳細信息,請參見here

0

你可以有一個線性佈局裏面,例如不同的看法和列表視圖:

?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="fill_parent" 
    android:orientation="vertical" 
    android:background="@drawable/background_new_search_activity_1"> 

<TextView 
      android:id="@+id/list_item_amount_text" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:cacheColorHint="#00000000"/> 

</LinearLayout> 

是的,你可以在所有的活動中使用的ListView,沒有必要延長ListActivity

3

線性佈局可以有任意數量的子項。 ListActivity是Android爲處理由列表組成的活動而添加的一個Activity。

您可以在常規Activity上使用ListView,只需實現一個適配器,該適配器將使用模型中的數據填充列表項。

Android有一些現成的適配器,可用於簡單的使用情況。 當然,如果你需要更復雜的beahviour,你可以擴展它們。

查看BaseAdapter,ArrayAdapterCursorAdapter以更好地瞭解如何使用適配器。

0

確保ListView的方向設置爲垂直。否則,它將嘗試並排顯示所有項目,而那些不在可用視圖區域內的項目將不可見。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical"> <!-- << orientation setting here -->