Android列表包装内容

时间:2013-01-07 10:23:52

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

我正在动态创建一些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值,那么它的show all list包含。

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

但我不能硬编码列表视图高度,因为我的内容是动态的。

如何在此方案中将列表视图设为WRAP_CONTENT

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:-1)

从文档ListView

  

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

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

为此,您最好使用垂直LinearLayout

相关问题