android-color - 在android中单击时更改itemView的颜色
问题描述
我想在点击时更改 itemView 的颜色,但无法这样做。我尝试了以下解决方案
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/color_change">
<ImageView
android:layout_width="130dp"
android:layout_height="90dp"
android:src="@drawable/download"
android:scaleType="centerCrop">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:textColor="@color/white"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:padding="2dp"
android:layout_marginRight="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="@color/black"
android:layout_marginRight="7dp">
</TextView>
</RelativeLayout>
color_change.xml 是
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:state_focused="false">
<shape>
<solid android:color="@color/white" />
</shape>
</item>
<item android:state_pressed="false" android:state_focused="true">
<shape>
<solid
android:color="@color/red" />
</shape>
</item>
<item android:state_pressed="true" android:state_focused="false">
<shape>
<solid
android:color="@color/grey" />
</shape>
</item>
我对另一个文件使用了相同的 xml,它工作正常。但这里没有做任何事情。
解决方案
推荐阅读
- android - Dagger/MissingBinding androidx.lifecycle.ViewModelProvider.Factory 不能在没有@Provides-annotated 方法的情况下提供
- javascript - 在没有 I/O 的情况下,javascript(在浏览器中)中的异步/承诺是否有益?
- python - 使用 PyInstaller 编译 python,用 Python
- ionic3 - Ionic4 与 Ionic3 中的离子段
- .net - 如何在空 ADT 帐户中创建 Azure 数字孪生备份并将其还原?
- vhdl - 在包内使用但未声明的组件
- java - 如何在recyclerview 中为togglebutton.setOnCheckedChangedListener 创建一个接口?
- python - 如何返回满足条件的字典中的最后一个值
- ldap - WSO2am 与 ldap AD 集成时抛出“尝试连接到端点时出错。无法为 ssl://localhost:7712 借用客户端”
- wso2 - 如果请求正文中不存在属性,有没有办法为属性中介设置默认值?