【问题标题】:Set max height for listview设置列表视图的最大高度
【发布时间】:2012-11-04 06:59:34
【问题描述】:

有些东西真的很愚蠢,但我想为我的列表视图设置一个最大高度,但我似乎找不到有用的东西。有人谈论为列表视图设置 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_weight="1" 作为列表视图,请看我的回答
  • @Talha 这在给定布局的上下文中无法实现。当 LinearLayout 中没有其他项时,layout_weight 无用。您至少需要两件物品才能达到预期的效果。

标签: android listview height


【解决方案1】:

试试这个:

 android:layout_height="wrap_content".

编辑: 您可以检查列表视图中的项目数量并设置大小以防其高于 20。类似于:

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

【讨论】:

    【解决方案2】:
    android:layout_height="0dp"
    android:layout_weight="1"
    

    【讨论】:

    • 或许有什么解释?在这种情况下设置的最大高度是多少? 1 个?
    【解决方案3】:

    android:layout="120dp" 一起或代替android:minHeight="20dp",假设20dp 是单个列表项的高度。这样,如果 ListView 有 1 或 2 个东西,它就不会占用整个 120dp。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 2019-03-25
      • 1970-01-01
      • 2018-07-22
      • 1970-01-01
      相关资源
      最近更新 更多