【问题标题】:StateListDrawable in ListView strangely off-colored in certain conditionsListView 中的 StateListDrawable 在某些情况下奇怪地变色
【发布时间】:2010-10-29 19:19:15
【问题描述】:

我创建了一个 ListView,其中 TextViews 作为项目 - 其中大多数具有某种颜色作为背景,但有些具有自定义的 @drawable/shape 作为背景(即顶部和底部项目,它们具有圆角)。 这些背景设置为状态列表的一部分,以便在点击项目时提供另一种背景颜色。 ListView 的 listSelector 设置为透明,而项目中包含的 TextView 设置为可点击。

列表视图:

<ListView android:id="@+id/allreminders_list" android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:dividerHeight="3px" android:cacheColorHint="#00000000"
android:divider="@color/trans" android:background="@drawable/reminderlist_background"
android:listSelector="@android:color/transparent"/>

ListView 条目:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"
android:id="@+id/entry_container">
<TextView android:id="@+id/remindername" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="dummy"
android:padding="15dp" android:layout_marginLeft="15px"
android:layout_marginRight="15px" style="@style/ReminderEntry"
android:clickable="true"/>
</RelativeLayout>

ListView入口背景的编程设置:

if (position == 1)
textViewReminderName
  .setBackgroundResource(R.drawable.reminder_entry_state_top);
else if (position == remindersInSection)
textViewReminderName
  .setBackgroundResource(R.drawable.reminder_entry_state_bottom);
else
textViewReminderName
  .setBackgroundResource(R.drawable.reminder_entry_state);

顶级项目的状态列表 (R.drawable.reminder_entry_state_top):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/rounded_top_corners_selected" />
<item android:state_selected="true" android:drawable="@drawable/rounded_top_corners_selected" />
<item android:drawable="@drawable/rounded_top_corners" />

未选中顶部项目的形状(@drawable/rounded_top_corners):

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="@color/entry_background"/>    
<corners android:bottomRightRadius="0dp" android:bottomLeftRadius="0dp" 
 android:topLeftRadius="5dp" android:topRightRadius="5dp"/> 
</shape>

这些东西在功能上有效。但是,顶部和底部项目(具有可绘制形状作为背景,如上所示)在某些条件下会稍微改变它们的颜色:

  • 在不点击停止滚动的情况下向下滚动列表后
  • 单击列表中的任何项目后

只有当我在滚动结束前不移开手指滚动列表时,才会显示这些项目的正确颜色。

这可能是什么? StateListDrawables 呈现可绘制形状的方式中的错误?

【问题讨论】:

  • 我做了更多的测试,这个问题只会在状态列表选择器与彩色形状组合时发生。当仅使用彩色形状或具有简单颜色的状态列表时,颜色保持完美。
  • 好的,与此同时,我已经为项目添加了切换按钮,还添加了使用形状(用于圆角)的状态列表选择器。猜猜看 - 每当顶部和最后一个项目颜色变淡(有点偏红)时,这也适用于那些切换按钮。太奇怪了..

标签: android listview colors


【解决方案1】:

经过无休止的研究和猜测,我自己找到了原因:抖动! 通过禁用状态列表选择器中的抖动,问题神奇地消失了:

<selector xmlns:android="http://schemas.android.com/apk/res/android" android:dither="false">

似乎抖动仅在某些条件下应用(如上所述),恕我直言,这不是预期的行为,但可以通过完全禁用它来治疗。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多