首页 > 解决方案 > 在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-color

解决方案


推荐阅读