【问题标题】:Android List wrap contentAndroid 列表包装内容
【发布时间】:2013-01-07 10:23:52
【问题描述】:

我正在动态创建一些TextViewImageViewHtmlView 并将其添加到以下布局中。

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

【问题讨论】:

  • 尝试我发布的解决方案。

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


【解决方案1】:

根据this Google I/O 2010 视频中看到的 Android 开发人员的说法,不能将 ListView 的高度设置为 wrap_content。如果是 ,则考虑前三个子元素的高度。其他的根本不包括在内。

尝试在运行时设置 ListView 的高度。或者最好将其设置为 FILL_PARENT。

【讨论】:

  • 是的,我尝试过使用 FILL_PARENT 但它不起作用,我正在尝试设置高度运行时。
【解决方案2】:

从文档中ListView

在垂直滚动列表中显示项目的视图

所以它不应该与WRAP_CONTENT 一起使用,而是在其中有一个固定的高度和滚动内容。

为此,您最好使用垂直的LinearLayout

【讨论】:

  • 我使用过 LinearLayout 但问题是,我在运行时添加了不同的视图,如文本视图、Web 视图等。我遇到了很多关于 webview 的问题,比如设置 Webview 高度,我需要为 textview 使用两个不同的侦听器 onclick ,为 webview 使用 onch ,多个 web view schooling,所以我选择 listview ,它解决了很多问题,如滚动、侦听器等。
  • 您希望列表可滚动还是包裹内容?
  • 我想要列表视图 jsut 显示他的所有单元格,目前它只显示一个单元格,如果我设置高度然后它显示所有单元格,而不是设置高度,它显示从他的内容中获取高度。所以我正在使用包装内容
  • 这意味着您不需要滚动列表,对吧?
  • 是的,我不需要滚动列表。
猜你喜欢
  • 2012-08-28
  • 2016-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-31
相关资源
最近更新 更多