微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 为listview设置最大高度

真的很蠢,但我想为listview设置一个最大高度,我似乎找不到有用的东西.有人谈到为listview设置maxheight,但我找不到这个选项?
我现在有

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="40dp" >

    <ListView
        android:id="@+id/lvSwitch"
        android:layout_width="fill_parent"
        android:layout_height="120dp" >
    </ListView>
</LinearLayout>

但是当列表中只有一个项目时,它仍然具有120dp的高度,这当然不是必需的,它只是不能超过那个高度……

解决方法

试试这个:

android:layout_height="wrap_content".

编辑:
你可以检查列表视图中的项目数量,并设置大小,以防它高于20.例如:

if(numberOfItems>20){
listview.setWidth(yourSize)
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐