android - 我怎样才能自动调整 ListView 的大小,使其不滚动

标签 android listview android-linearlayout

目前我有以下布局

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 

    android:layout_marginTop="9px" 
    android:layout_below="@+id/desc" 
    android:id="@+id/ll_item"  
    android:orientation="vertical" 
    android:paddingRight="3px" 
    android:paddingLeft="3px" 
    android:paddingBottom="5px" 
    android:paddingTop="5px" 
    android:background="@drawable/rounded_corner_lists" >
<!--
        <ListView android:drawSelectorOnTop="false" android:id="@+id/lv"    android:layout_height="fill_parent" android:layout_width="wrap_content" android:divider="#ddd" android:dividerHeight="1px" android:background="@drawable/white"    />
    -->
    </LinearLayout>

我已经注释掉的 ListView ,我试图在 xml 中制作它,高度设置为 wrap_content,fill_parent,目前我正在使用以下代码以编程方式执行此操作

LinearLayout ll_item = (LinearLayout) this.findViewById(R.id.ll_item);
        if(list.length() > 0)
        {
            ll_item.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,calcListHeight(list));

            listview = new ListView(this);
            listview.setBackgroundResource(R.drawable.white);
            listview.setDivider( new ColorDrawable(this.getResources().getColor(R.drawable.dividercolor)) );
            listview.setDividerHeight(1);
            listview.setCacheColorHint(0);

            mAdapter  = new JSONAdapter( list, this );
            listview.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
            ll_item.addView(listview, lp);
        }

这是结果

alt text

alt text

因此您可以在这张图片中看到,由于我将 ListView 包含在线性布局中以获得圆角外观,它不会自动拉伸(stretch)以包含整个 ListView ,有没有办法让这两个元素只是垂直包装内容,所以如果没有我以编程方式设置高度就不会滚动? ? ?

我想我应该提到的另一件事是我将所有这些布局都放在一个 ScrollView 中,因为我希望这个 ListView 是整个布局的一小部分,所以它会像

- ScrollView
- TextView
- TextView

-线性布局
- ListView

- 按钮

这是我所拥有的更简单的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent"
              android:background="@drawable/bg" xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/titlebar">

    <ScrollView android:id="@+id/sv" android:layout_width="fill_parent" android:background="@drawable/bg"
                android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

        <RelativeLayout android:id="@+id/widget28"
                        android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="4dip"
                >


            <LinearLayout android:orientation="vertical" style="@style/rounded_corner_full_width_button"
                          android:id="@+id/editfields">

                <ListView android:drawSelectorOnTop="false" android:id="@+id/lv" android:layout_height="fill_parent"
                          android:layout_width="wrap_content" android:divider="#ddd" android:dividerHeight="1px"
                          android:background="@drawable/white"/>

            </LinearLayout>
        </RelativeLayout>
    </ScrollView>
</LinearLayout>

最佳答案

ListViews 不进入 ScrollViews。

ListView 用于有效地将有限的窗口显示为无限的内容。如果您要在 ListView 上“禁用滚动”以将其放在 ScrollView 中,那么您首先就失去了使用 ListView 的所有实际理由。

如果您想使用 ListView 显示大量内容或无限制的内容,但也有上下滚动的内容,请使用 addHeaderView 向 ListView 添加页眉或页脚 View 。或 addFooterView .如果列表内容如您所描述的那样只占整体布局的一小部分,那么这可能不是最适合您的方法。

如果您要呈现一组小的、有界的内容,请继续使用 ScrollView 并在适当的地方以编程方式为您的“列表项”生成 subview 。

框架中用于将膨胀的 XML 内容与以编程方式生成的内容混合的常见模式是在布局 XML 中添加占位符 View ,通常是 LinearLayoutFrameLayout。使用 findViewById 在运行时定位它并将生成的 subview 添加到它。

如果您已经编写了一个 ListAdapter,您甚至仍然可以使用这种方法,只需调用 content.addView(adapter.getView(position, null, content)) 在所有适配器位置的循环中(其中 content 是您使用 findViewById 定位的占位符 View )。请注意,这仅在您知道适配器中的列表项数量很少时才实用!

关于android - 我怎样才能自动调整 ListView 的大小,使其不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3506103/

相关文章:

java - 用于打开其他应用程序并充当查看器的应用程序

javascript - ListView getListItemXmlAttributes 方法因子发布项而失败

android - 如何在一行中对齐 3 个按钮,android?

android - 亚马逊服务异常 : User is not authorized to perform: dynamodb:DescribeTable Status Code: 400; Error Code: AccessDeniedException

java - ERROR : In <declare-styleable> NavArgument, 找不到属性类型

android - 对 ListView 中的项目使用 setOnItemClickListener

java - 该方法在 ListView 中应用了错误的元素

Android:如何进行垂直合并?

android - 如何在线性布局周围创建边框,如下图所示?

android - 如何自动调整用作图层列表中可绘制项目的图像高度?