OnTouch在XML中更改图像 - android

时间:2014-08-01 12:25:38

标签: xml imageview selector

我在drawable文件夹中有一个xml文件,我想用它作为ImageView src。这是代码:

<?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/home2" />
<item android:state_pressed="false" android:drawable="@drawable/home1"></item>
</selector>

正如您所看到的,我希望每次按下图像时,它都会将图像更改为另一张图像但由于某种原因它无法正常工作。它只显示默认图像,当我按下图像时,它不会被切换。

另外,我尝试使用此代码,但得到的结果相同:

<?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/home2" />
<item android:drawable="@drawable/home1"></item>
</selector>

可以解决什么问题?

1 个答案:

答案 0 :(得分:0)

你的意思是例如在按下图像= home1时按下图像= home2,当用户释放按image = home2时它不会再次改变。 如果是这样,您可以使用此代码。

<item android:state_pressed="true"
    android:drawable="@drawable/home2" />
相关问题