android-为什么ColorStateList的按下状态不能与ListView项一起使用?

android-为什么ColorStateList的按下状态不能与ListView项一起使用?,第1张

概述我正在尝试使用具有不同背景颜色的项目设置ListView,因此我适配器的getView()方法调用setBackgroundResource(),并为所需的背景颜色使用适当的可绘制资源.如果我使用引用ColorStateList作为其颜色的ColorDrawable,则在我点击该项目时,不会绘制ColorStateList中按下状态的颜色.如

我正在尝试使用具有不同背景颜色的项目设置ListVIEw,因此我适配器的getVIEw()方法调用setBackgroundResource(),并为所需的背景颜色使用适当的可绘制资源.

如果我使用引用colorStateList作为其颜色的colorDrawable,则在我点击该项目时,不会绘制colorStateList中按下状态的颜色.

如果我使用一个StateListDrawable,它为按下状态引用一个colorDrawable,而为未按下状态引用一个不同的colorDrawable,则当我点击该项目时,将获得所需的突出显示效果.

我已经建立了一个简单的项目来演示这一点.这是listadapter的getVIEw()方法:

@H_419_11@@OverrIDepublic VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {    VIEw retval = getLayoutInflater().inflate(androID.R.layout.simple_List_item_1, parent, false);    TextVIEw textVIEw = (TextVIEw)retval.findVIEwByID(androID.R.ID.text1);    textVIEw.setText("" + position);    switch ( position ) {        case 0:            retval.setBackgroundResource(R.drawable.List_background_item_0);            break;        case 1:            retval.setBackgroundResource(R.drawable.List_background_item_1);            break;    }    return retval;}

res / drawable / List_background_item_0.xml:

@H_419_11@<color xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:color="@color/List_background_item_0" />

分辨率/颜色/List_background_item_0.xml:

@H_419_11@<selector xmlns:androID="http://schemas.androID.com/apk/res/androID">    <item androID:state_pressed="true" androID:color="@color/List_background_item_0_pressed"/>    <item androID:color="@color/List_background_item_0_default"/></selector>

res / drawable / List_background_item_1.xml:

@H_419_11@<selector xmlns:androID="http://schemas.androID.com/apk/res/androID">    <item androID:state_pressed="true" androID:drawable="@drawable/List_background_item_1_pressed" />    <item androID:drawable="@drawable/List_background_item_1_default" /></selector>

res / drawable / List_background_item_1_default.xml:

@H_419_11@<color xmlns:androID="http://schemas.androID.com/apk/res/androID"     androID:color="@color/List_background_item_1_default" />

res / drawable / List_background_item_1_pressed.xml:

@H_419_11@<color xmlns:androID="http://schemas.androID.com/apk/res/androID"     androID:color="@color/List_background_item_1_pressed" />

res / values / colors.xml:

@H_419_11@<resources>    <color name="List_background_item_0_default">#FFCCCC</color>    <color name="List_background_item_0_pressed">#996666</color>    <color name="List_background_item_1_default">#CCFFCC</color>    <color name="List_background_item_1_pressed">#669966</color></resources>

列表项0配置有colorDrawable,该colorDrawable引用colorStateList并且在按下时不显示突出显示.列表项1配置有一个StateListDrawable并显示突出显示.

我宁愿使用colorStateList而不是StateListDrawable,因为它看起来更干净,并且每个项目类型所涉及的文件减少了一个.有什么我想念的东西可以让我使用这种方法吗?如果它不是我期望的那样工作,有人可以解释为什么吗?

解决方法:

感谢Luksprog的注释,我了解到StateListDrawable引用的可绘制对象可以是colors.xml中的颜色.这使得StateListDrawable解决方案优于colorStateList版本.

最终的解决方案是…

getVIEw()方法:

>没有变化

分辨率/颜色/List_background_item_0.xml:

>已删除

res / drawable / List_background_item_0.xml:

@H_419_11@<selector xmlns:androID="http://schemas.androID.com/apk/res/androID">    <item androID:state_pressed="true" androID:drawable="@color/List_background_item_0_pressed" />    <item androID:drawable="@color/List_background_item_0_default" /></selector>

res / drawable / List_background_item_1.xml:

@H_419_11@<selector xmlns:androID="http://schemas.androID.com/apk/res/androID">    <item androID:state_pressed="true" androID:drawable="@color/List_background_item_1_pressed" />    <item androID:drawable="@color/List_background_item_1_default" /></selector>

res / drawable / List_background_item_1_default.xml:

>已删除

res / drawable / List_background_item_1_pressed.xml:

>已删除

res / values / colors.xml:

>没有变化

这个解决方案比我预期的colorStateList版本可以使用的清洁或干净.

总结

以上是内存溢出为你收集整理的android-为什么ColorStateList的按下状态不能与ListView项一起使用?全部内容,希望文章能够帮你解决android-为什么ColorStateList的按下状态不能与ListView项一起使用?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1088098.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存