如何自动调整listview的大小,使其不滚动

sli*_*lim 8 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>
Run Code Online (Sandbox Code Playgroud)

我已经注释掉的列表视图,我试图在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);
        }
Run Code Online (Sandbox Code Playgroud)

结果就是这样

替代文字

替代文字

所以你可以在这张图片中看到,因为我在一个linearlayout中包含了listview以获得圆角的外观,它不只是自动拉伸以包含整个listview,有没有办法让这两个元素只是换行垂直内容所以没有滚动没有我以编程方式设置高度???

我想另外一件事我应该提到的是我在scrollview中拥有所有这些布局,因为我希望这个listview是整个布局的一个小部分,所以它会像

-scrollview
-textview
-textview

-linearlayout
-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>
Run Code Online (Sandbox Code Playgroud)

ada*_*amp 18

ListViews不会进入ScrollViews.

ListView用于有效地将有限窗口显示为无界内容.如果你要在ListView上"禁用滚动"将它放在ScrollView中,你就会失去首先使用ListView的所有实际原因.

如果您想使用ListView来显示大量内容或无限制内容,但也有使用它滚动的内容,请使用addHeaderView或向ListView添加页眉或页脚视图addFooterView.如果列表内容将是您所描述的整体布局的一小部分,那么这可能不是您的最佳方法.

如果您要显示一小组有限的内容,请继续使用ScrollView并以适当方式为您的"列表项"以编程方式生成子视图.

框架中用于将膨胀的XML内容与编程生成的内容混合的常见模式是在布局XML中添加占位符视图,通常是a LinearLayoutFrameLayout.用于findViewById在运行时定位它并向其添加生成的子视图.

ListAdapter如果你已经有一个写入,你甚至可以使用这种方法,只需content.addView(adapter.getView(position, null, content))在一个循环中调用所有适配器位置(content你找到的占位符视图findViewById).请注意,只有当您知道适配器中有少量列表项时,这才是实用的!