0

我正在創建一些TextView,ImageView,HtmlView動態並將其添加到以下佈局中。Android列表包裝內容

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/parent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:id="@+id/ad_space" 
     android:layout_width="fill_parent" 
     android:layout_height="40dp" 
     android:background="@drawable/navigation_bar" 
     android:visibility="gone" > 
    </LinearLayout> 

    <ViewFlipper 
     android:id="@+id/viewflipper" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

     <ScrollView 
      android:id="@+id/quiz_scroll_view" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 

      <LinearLayout 
       android:id="@+id/quizView" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" > 

       <LinearLayout 
        android:id="@+id/answer_layout" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" /> 
      </LinearLayout> 
     </ScrollView> 

     <ScrollView 
      android:id="@+id/quiz_scroll_viewTwo" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 

      <LinearLayout 
       android:id="@+id/quizViewTwo" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" > 

       <LinearLayout 
        android:id="@+id/answer_layoutTwo" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" /> 
      </LinearLayout> 
     </ScrollView> 
    </ViewFlipper> 
</LinearLayout> 

QuizView我加入了在運行時的一些問題有關的意見,答案我創建一個列表視圖運行時間,並增加answer_layout。我正在使用BaseAdapter根據情況創建ListView運行時。

但我的ListView未顯示所有內容,它只顯示第一個單元格。

我已添加ListView這樣。

ListView ansList = new ListView(Test.this); 
ansList.setAdapter(adapter); 
ansList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
ansCellLayout.addView(ansList); 

它只顯示一個列表單元格。

如果我設置了一些int值的高度,那麼它的顯示所有列表包含。

ansList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 600)); 

但我不能硬編碼列表視圖高度,因爲我的內容是動態的。

如何在此場景中將列表視圖設置爲WRAP_CONTENT

+0

試試我發佈的解決方案。 –

回答

2

根據Android開發人員在this Google I/O 2010視頻中看到,ListView的高度不能設置爲wrap_content。當它是,前三個子元素被認爲是高度。其他人根本不包括在內。

嘗試在運行時設置ListView的高度。或者更好的將其設置爲FILL_PARENT。

+0

是的,我已經嘗試過使用FILL_PARENT,但它的劑量工作,我可以設置高度運行時。 – Mac

-1

從文檔一個ListView

,顯示在垂直滾動列表

所以項目的視圖它不應該與WRAP_CONTENT使用,而是有一個固定的高度和滾動裏面的內容。

您最好使用垂直LinearLayout用於此目的。

+0

我已經使用LinearLayout,但它的問題是,我在運行時添加不同的視圖像文本視圖,Web視圖等我已經面臨很多與webview的問題設置Webview高度,我需要使用兩個不同的監聽器onclick textview,爲webview多個web視圖,多個網絡視圖學校,所以我選擇listview,它解決了很多問題,如滾動,聽衆等。 – Mac

+0

你想列表滾動或包裝內容? – fiddler

+0

我想列表視圖jsut顯示他所有的細胞,目前它只顯示一個細胞,如果我設置高度,那麼它顯示的所有細胞,而不是設置高度,它顯示從他的內容高度。所以我用包裝內容 – Mac