【问题标题】:Show the Entire ListView in a LinearLayout在 LinearLayout 中显示整个 ListView
【发布时间】:2012-01-01 17:33:01
【问题描述】:

我尝试在WebView 下放置一个ListView,为此我使用以下xml 代码:

<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项目的1.5倍左右,所以没有显示整个列表,而只显示了第一个项目.

我在LinearLayoutWebViewListView 和周围的ScrollView 中尝试了android:layout_height="fill_parent"android:layout_height="wrap_content" 的几种组合,但都没有奏效。

【问题讨论】:

  • 抱歉,我没有回答您的问题,但有一个观察结果……将可滚动的 UI 元素放入 ScrollView 确实不是一个好主意,并且可能导致不可预测的行为。 WebViewListView 都可以(或可以)滚动。
  • 我知道这个解决方案一点都不好,但在this 问题中我要求另一种方式,但我很高兴我找到了任何东西。
  • 列表的来源是什么?它是可变的还是您想要显示的项目数量是固定的?如果它是固定的,有多少项目?
  • 我正在用一个字符串数组对其进行测试,但如果它有效,我将用一个数组列表替换它,其中包含论坛文章的 cmets,因此每次的数字都不相同。
  • 但如果它是可变的,那么您将永远不会一次看到所有项目 - 这就是 ListView 可滚动的重点 - 适配器可以引用 100 或 1000 个项目和 @ 987654339@ 将在任何时候显示一个小(有限)数字,但允许用户滚动浏览总数。如果您希望所有项目都出现在WebView 下方(在ScrollView 内),请尝试动态创建项目视图并将其添加到ScrollView,例如,每个项目一个TextView。我仍然认为在ScrollView 中使用WebView 是个坏主意。

标签: android listview android-layout layout android-linearlayout


【解决方案1】:

您应该使用布局权重。这是一种使用百分比来定义布局的方法。

在以下示例中,ListView 使用 60% 的空间,而 WebView 使用 40%:

<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>

只需将这些百分比更改为您想要的任何值。

【讨论】:

  • 这不起作用,我尝试了几个值,但我认为权重在 ScrollView 中不起作用。
  • 您可以尝试为您的两个视图设置固定高度。如果您想使用 ScrollView,我认为没有其他方法。例如,您可以通过获取屏幕高度以编程方式完成,并将其设置为每个视图的高度。
  • 但我认为你应该听从 MisterSquonk 的建议,忘记这个布局。 IMO 在此之后您将遇到另一个问题,即使您得到了您想要的...
  • 我已经有一个解决方案,显示的上半部分是WebView,下半部分是List,但是我想显示一个WebView,而在WebView 的末尾是List。两者都应该使用一个滚动条。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多