用于ListView高度和宽度的wrap_content

时间:2012-12-13 07:15:32

标签: android android-layout listview

我想限制ListView的高度和宽度,根据子项的数量包装高度,并根据列表视图行的宽度包装宽度。

到目前为止,我已经能够限制ListView的高度。我用来限制身高的代码是:

listview.setLayoutParams(new linealayout.layoutparams(LayoutParams.WRAP_CONTENT, 0,1));

我怎样才能限制宽度?

1 个答案:

答案 0 :(得分:0)

我希望您能更加具体地了解所需的屏幕外观/视图

但是当谈到listview我会说从不使用20,30 dp设置宽度和高度只需使用weight和wrap_content来使其灵活。

请参阅下面的代码示例,这可能会对您有所帮助

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
    <ListView
        android:layout_height="wrap_content"
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_weight="1"></ListView>
    <Button
        android:text="Button"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Button>

</LinearLayout>

如果你仍然有任何麻烦,请告诉我。

相关问题