RecyclerView碰上selector

Krisez 2020-08-10 03:21:23
机型 红米note4 android6
问题:
recyclerview的item内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/select_bg"
android:gravity="center_vertical"
android:textSize="15sp" />
</LinearLayout>


background内容如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/grey"/>
</selector>

现在呢,点击item会有一个按压的效果。

又因为背景色只是颜色,所以把`select_bg`文件移动到`color`文件夹下
然后内容这样子:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/grey" android:state_pressed="true" />
<item android:color="@color/color_white" android:state_pressed="false" />
</selector>

item的xml代码backgound也改成了对应的
android:background="@color/select_bg"

接着开始运行代码,出问题了,在我这个机型下,它崩溃了,当我这段代码在模拟机上(androidR)运行又正常了。

麻烦知道原因的大佬讲解一下呢。。谢谢

recyclerview版本 support 28
...全文
3232 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_42159713 2020-08-11
  • 打赏
  • 举报
回复
好东西呀!!!!!!!
立青_ 中级 2020-08-11
  • 打赏
  • 举报
回复
在个别手机上好像不支持背景是颜色状态选择器,可以
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/select_bg" />
</shape>
然后 android:background =“@drawable/名字”
Krisez 2020-08-11
  • 打赏
  • 举报
回复
引用 3 楼 立青_ 的回复:
在个别手机上好像不支持背景是颜色状态选择器,可以
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/select_bg" />
</shape>
然后 android:background =“@drawable/名字”
厉害了,这次小米没崩
Krisez 2020-08-10
  • 打赏
  • 举报
回复
这东西碰上了 贼蛋疼
ink_s 2020-08-10
  • 打赏
  • 举报
回复
不知道,没放color试过,我一直都是放drawable的。

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧