如何在java文件中设置列表视图的高度?

时间:2011-08-22 05:42:16

标签: android listview

您好我从网上获取列表视图的数据,并希望根据列表视图中的元素数据没有修复列表视图的高度,所以我尝试下面的代码,但它在运行时给出错误,所以任何一个人可以帮我解决这个问题,谢谢

你可以在这里看到我的xml文件

<?xml version="1.0" encoding="utf-8"?>

                                                                                          

<ScrollView android:id="@+id/sos_scroll"
    android:layout_above="@+id/last_lin" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_below="@+id/date"
    android:layout_marginTop="5dip" android:fadingEdge="none"
    android:scrollbars="none">

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

        <ListView android:paddingLeft="1dip" android:scrollbars="none"
            android:cacheColorHint="#00000000" android:focusable="true"
            android:id="@+id/list" android:dividerHeight="2dip"
            android:layout_below="@+id/date" android:footerDividersEnabled="false"
            android:headerDividersEnabled="false" android:layout_marginRight="12dip"
            android:listSelector="@android:color/transparent"
            android:background="@drawable/newshape_list"
            android:layout_marginLeft="12dip" android:paddingRight="1dip"
            android:layout_height="wrap_content" android:layout_width="fill_parent"
            android:fadingEdge="none" android:divider="#A9ABAD"></ListView>

        <RelativeLayout android:layout_width="wrap_content"
            android:layout_marginLeft="10dip" android:layout_marginBottom="5dip"
            android:layout_marginRight="10dip" android:layout_height="wrap_content"
            android:layout_below="@+id/list">
            <TableLayout android:layout_width="wrap_content"
                android:id="@+id/add_table" android:layout_centerHorizontal="true"
                android:background="@drawable/listback" android:layout_height="wrap_content"
                android:stretchColumns="1">
                <TableRow android:id="@+id/add_data">
                    <ImageView android:layout_width="wrap_content"
                        android:src="@drawable/addbuttonimage" android:id="@+id/que1"
                        android:layout_height="wrap_content"></ImageView>
                    <TextView android:text="Add New Stress..."
                        android:layout_width="wrap_content" android:textStyle="bold"
                        android:textColor="#000000" android:layout_marginLeft="5dip"
                        android:textSize="15sp" android:layout_height="wrap_content"
                        android:padding="3dip" />
                </TableRow>
            </TableLayout>
        </RelativeLayout>
    </RelativeLayout>
</ScrollView>
<LinearLayout android:id="@+id/last_lin"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:paddingBottom="52dip" android:paddingTop="2dip"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true">
    <Button android:layout_width="wrap_content" android:textColor="#FFFFFF"
        android:id="@+id/done" android:textStyle="bold" android:text="Save"
        android:layout_height="wrap_content" android:background="@drawable/savebutton"></Button>
</LinearLayout>

错误就在这里

08-22 11:00:34.930: ERROR/AndroidRuntime(324): Uncaught handler: thread main exiting due to uncaught exception
08-22 11:00:34.959: ERROR/AndroidRuntime(324): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1291)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1238)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:279)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:299)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:893)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.ScrollView.onMeasure(ScrollView.java:276)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:569)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:361)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at  android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:569)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:361)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.View.measure(View.java:7964)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.os.Looper.loop(Looper.java:123)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at java.lang.reflect.Method.invokeNative(Native Method)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at java.lang.reflect.Method.invoke(Method.java:521)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-22 11:00:34.959: ERROR/AndroidRuntime(324):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您是否考虑过完全动态创建ListView?换句话说,尝试以编程方式创建ListView,然后手动将其添加到布局中。

另外,您是否有任何理由想要手动指定ListView高度?您的ListView将根据您拥有的数据量自动调整大小,如果您拥有的数据超出了屏幕上可以容纳的数据,它将变为可滚动。

修改

在参考您的第二条评论和您所包含的XML布局时,我建议您这样做。

您的XML文件应该只有一个根元素(因此它被称为根元素)。目前,您有一个ScrollView和一个LinearLayout。我建议你只使用一个LinearLayout作为根。您可以将其用作粗略的骨架:

<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  ... >

    <ListView
        ...
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1" />
    <TableLayout
        ...
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:layout_weight="0" />
    <Button
        ...
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:layout_weight="0" />

</LinearLayout>

此布局基本上将您的Button推到最底部,将TableLayout推到底部,但在Button上方。 ListView将占用TableLayout上方的其余空间。 (顺便说一句,TableRow基本上是一个专门的LinearLayout。你可以简单地用一个水平的LinearLayout替换TableLayout来实现类似的结果,除非你打算在表中添加多行并且需要对列进行相同的大小调整。)