【问题标题】:ListView divider not showing in Android 5Android 5 中未显示 ListView 分隔符
【发布时间】:2015-01-08 17:03:38
【问题描述】:

我有一个简单的列表视图,我为它定义了一个自定义可绘制的分隔线。我已将分隔线高度定义为 1dp。列表视图位于片段内。

<shape
    android:shape="line" >
    <stroke
        android:color="@color/custom_color" />

    <gradient android:height="1dp" />

</shape>

它适用于除 L 之外的所有 Android 版本。

我缺少什么吗?

【问题讨论】:

  • 我会尝试“2dp”,否则我不知道会丢失什么。
  • 我也试过 4dp。没有帮助。
  • listview.setdivider(R.drawable.line);
  • 尝试使用 px 而不是 dp,因为对于 ldpi,1dp = 0.75 像素,所以它向下舍入为 0,并且不会绘制分隔线。
  • 好点。但我在 Nexus 5(使用 xxhdpi 资产)上运行它

标签: android android-layout android-listview android-5.0-lollipop


【解决方案1】:

您应该使用android:shape="rectangle" 而不是android:shape="line" 以使其适用于每个android 版本...(也将stroke 更改为solid

<shape
    android:shape="rectangle" >
    <solid android:color="@color/custom_color" />
    <gradient android:height="1dp" />
</shape>

玩得开心!

【讨论】:

    【解决方案2】:

    您的列表项是否通过覆盖 isEnabled() 以返回 false 被禁用? Android L 中有一个更改(错误?),如果项目被禁用,则会导致列表项目分隔符被隐藏。我遇到了同样的问题,我的列表分隔符在除 L 之外的所有内容中都起作用,结果证明这是原因。

    这里还有一些讨论过这个问题的帖子,以及 Google 提出的一个问题:

    在这里评论:Disappearing divider in ListView when ArrayAdapter.isEnabled returns false

    How to add dividers between disabled items in ListView? - Lollipop

    https://code.google.com/p/android/issues/detail?id=83055

    如果是这种情况,听起来您可能需要使用自定义视图手动绘制分隔线,并将列表中的分隔线设置为 null。我也会尝试的。

    【讨论】:

      【解决方案3】:

      更新答案

      经过进一步测试,似乎只有当分隔线的高度严格小于小于为 ListView 设置的dividerHeight 时,分隔线才会显示。例如:

      custom_divider.xml (注意分隔线高度由android:width指定)

      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="line" >
          <stroke
              android:width="1dp"
              android:color="$ffff0000" />
      </shape>
      

      布局xml

      <ListView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/listView"
          android:divider="@drawable/custom_divider"
          android:dividerHeight="2dp"/>
      

      ...会起作用的。但这不会:

      custom_divider.xml (注意分隔线高度由android:width指定)

      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="line" >
          <stroke
              android:width="1dp"
              android:color="$ffff0000" />
      </shape>
      

      布局xml

      <ListView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/listView"
          android:divider="@drawable/custom_divider"
          android:dividerHeight="1dp"/>
      

      我的猜测是 Google 搞砸了绘制 Listview 分隔线的优化,如果没有足够的空间就不会绘制它们。

      原帖

      看起来您需要同时设置 ListView 上的 dividerHeight 和可绘制分隔线的笔划 width 才能在 Android 5 上运行。

      例子:

      custom_divider.xml

      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="line" >
          <stroke
              android:width="10dp"
              android:color="$ffff0000" />
      
          <gradient android:height="1dp" />
      </shape>
      

      布局xml

      <ListView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/listView"
          android:divider="@drawable/custom_divider"
          android:dividerHeight="20dp"/>
      

      【讨论】:

      • 感谢您的解决方案!!
      【解决方案4】:

      Height 属性不是渐变标签下的属性。使用如下尺寸属性。

      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle" >
      
          <solid android:color="@android:color/holo_blue_dark" />
      
          <size android:height="1px" />
      
      </shape>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-05
        • 1970-01-01
        • 1970-01-01
        • 2021-06-03
        相关资源
        最近更新 更多