在drawable 目录下增加一个myselector.xml文件,appwidget_play.png,appwidget_played.png
myselector.xml文件中内容如下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false"
android:drawable="@drawable/appwidget_play" /> <!-- 正常情况下imagebutton效果 -->
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/appwidget_played" /> <!-- 按下后imagebutton效果 -->>
</selector>
layout文件中:
<ImageButton android:layout_height="wrap_content"
android:background="#00000000" android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:src="@drawable/myselector"
activity文件中:
iImageButton = (ImageButton) findViewById(R.id.imageButton1);
这里不需要在设置iImageButton.setImageDrawable(getResources().getDrawable(R.drawable.appwidget_play));
ImageButton动态改变按钮图片