我知道如何实现“点击图片就令其改变颜色”(即android:state_pressed="true"),但却不知道如何实现“把鼠标悬停在上面令其改变颜色”——android:state_hovered="true"并不能达到我想要的效果,我把鼠标悬停在上面什么反应都没有。以下是我成功实现“点击图片令其改变颜色“的代码,应该如何修改,才能让我鼠标悬停在图片上就令其改变颜色呢?
以下是activity_main的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/state_list" />
</RelativeLayout>
以下是drawable下的state_list.xml:
<?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/childHovered" /> <!-- pressed -->
<item android:drawable="@color/childDefault" /> <!-- default -->
</selector>
以下是value下的colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="childDefault">#4b2a45</color>
<color name="childHovered">#ff0000</color>
</resources>