说一下我遇到过的两个原因:
第一种:相信大家都遇到过,就是顺序问题。正常状态要放到下面,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/dialog_btn_pressed"/>
<item android:drawable="@drawable/dialog_btn_normal" android:state_pressed="false"/>
</selector>
如果把按压的状态放到下面,就不会起作用。
第二种:与采用的主题有关
本人曾经采用过Theme.MaterialComponents.Light.NoActionBar主题,结果导致selector失效,解决办法就是更换其他主题,可以更换为Theme.AppCompat.Light.NoActionBar主题。不熟悉的主题不要乱用。

本文分享了在Android开发中遇到的两种资源选择器(selector)失效的问题。第一种情况是顺序错误,当按压状态的item放在正常状态item之后时,可能导致按压效果无法显示。第二种情况是主题选择不当,使用Theme.MaterialComponents.Light.NoActionBar可能导致selector不起作用,更换为Theme.AppCompat.Light.NoActionBar可解决问题。了解这些细节有助于避免在开发中浪费时间。
690

被折叠的 条评论
为什么被折叠?



