简体   繁体   English

ListView分隔符未在Android 5中显示

[英]ListView divider not showing in Android 5

I have a simple listview for which I have defined a custom drawable for the divider. 我有一个简单的列表视图,我已经为分隔符定义了一个自定义drawable。 I have defined the divider height to be 1dp. 我已将分隔符高度定义为1dp。 The listview is within a fragment. 列表视图位于片段内。

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

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

</shape>

It works great for all Android versions except L. 它适用于除L以外的所有Android版本。

Anything I'm missing? 我缺少什么?

You should use android:shape="rectangle" instead of android:shape="line" to make it work on every android version... (also change stroke to solid ) 您应该使用android:shape="rectangle"而不是android:shape="line"使其适用于每个Android版本...(也将stroke更改为solid

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

Have fun! 玩得开心!

By any chance, is your list item disabled via overriding isEnabled() to return false? 在任何情况下,您的列表项是否通过重写isEnabled()来禁用,以返回false? There is a change (bug?) in Android L that causes list item dividers to be hidden if an item is disabled. Android L中存在更改(错误?),如果项目被禁用,则会导致列表项目分隔符被隐藏。 I ran into this same issue, my list dividers working in everything but L, and this turned out to be the cause. 我遇到了同样的问题,我的列表分隔符在L以外的所有内容中工作,结果证明这是原因。

Here are some more posts where this has been discussed, as well as an issue opened up with Google: 以下是一些讨论过的帖子,以及Google开放的问题:

Comments here: Disappearing divider in ListView when ArrayAdapter.isEnabled returns false 注释: 当ArrayAdapter.isEnabled返回false时,ListView中的消除分隔符

How to add dividers between disabled items in ListView? 如何在ListView中的禁用项之间添加分隔符? - Lollipop - 棒棒糖

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

If this is the case, it sounds like you may need to manually draw the divider with a custom view and set the divider to null on the list. 如果是这种情况,听起来您可能需要使用自定义视图手动绘制分隔符,并在列表中将分隔符设置为null。 I will be trying the same. 我会尝试同样的。

Updated answer 更新的答案

After further testing it appears that the dividers will only show if the height of the divider is strictly less than the dividerHeight set for the ListView. 经过进一步测试后,看起来分隔符只显示分频器的高度是否严格小于为ListView设置的dividerHeight For instance: 例如:

custom_divider.xml (Note that the divider height is specified by android:width ) 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>

Layout xml 布局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"/>

...Will work. ...将工作。 But this will not: 但这不会:

custom_divider.xml (Note that the divider height is specified by android:width ) 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>

Layout xml 布局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"/>

My guess is that Google messed up with the optimization for drawing Listview dividers and will simply not draw them if there is not enough room. 我的猜测是谷歌搞砸了绘制Listview分隔线的优化,如果没有足够的空间,就不会绘制它们。

Original post 原帖

Looks like you need to both set the dividerHeight on the ListView and the stroke width of the divider drawable for this to work on Android 5. 看起来你需要在ListView上设置dividerHeight ,并且可以在Android 5上使用分隔符的笔触width

Example: 例:

custom_divider.xml 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>

Layout xml 布局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"/>

Height attribute is not a property under gradient tag. 高度属性不是渐变标记下的属性。 Use size attr like below. 使用大小attr如下。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM