1

我嘗試把一個ListViewWebView,要做到這一點,我用folloing XML代碼:顯示整個ListView控件中的LinearLayout

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/bg" 
    android:orientation="vertical" 
    android:scrollbars="vertical" > 

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

     <WebView 
      android:id="@+id/content" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:cacheColorHint="#00000000" 
      android:textColor="#FFDEC2" /> 

     <ListView 
      android:id="@+id/comments" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" > 
     </ListView> 
    </LinearLayout> 

</ScrollView> 

代碼正確顯示WebViewListView的內容,但ListView只有ListView Item的約1.5倍,因此不是顯示整個列表,而是僅顯示第一個項目。

我在LinearLayoutWebViewListView和周圍ScrollView試過android:layout_height="fill_parent"android:layout_height="wrap_content"幾種組合,沒有一次成功。

+0

對不起,我沒有你的問題的答案,但一個觀察...把可滾動的UI元素放入一個'ScrollView'真的不是一個好主意,並可能導致不可預知的行爲。 「WebView」和「ListView」都是(或可以)可滾動的。 – Squonk

+0

我知道這個解決方案不是很好,但在[這個](http://stackoverflow.com/questions/8152071/a-listview-under-a-webview)問題我問另一種方式,但我'我很高興我能找到任何東西。 – MWeller

+0

列表的來源是什麼?它是可變的還是你有固定數量的項目要顯示?如果它是固定的,那裏有多少物品? – Squonk

回答

3

您應該使用佈局權重。這是使用百分比來定義佈局的一種方法。

在下面的例子中,ListView控件使用的空間的60%,和40的WebView%:

<LinearLayout 
    android:id="@+id/main_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:weightSum="1.0" > 

    <WebView 
     android:id="@+id/content" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:cacheColorHint="#00000000" 
     android:textColor="#FFDEC2" 
     android:layout_weight=".40" /> 

    <ListView 
     android:id="@+id/comments" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight=".60" > 
    </ListView> 
</LinearLayout> 

只要改變任何你想要這些百分比。

+0

這不工作,我嘗試了幾個值,但我認爲重量不工作在ScrollView中。 – MWeller

+0

您可以嘗試爲兩個視圖設置修正高度。如果你想使用ScrollView,我認爲沒有其他的方法。例如,您可以通過獲取屏幕高度以編程方式進行設置,並將其設置爲每個視圖的高度。 – Dalmas

+0

但我認爲你應該遵循MisterSquonk的建議,並忘記了這種佈局。國際海事組織,你會在這之後有另一個問題,即使你得到你想要的... – Dalmas