使用代码更改图像
原文链接:https://www.f2er.com/xml/293608.htmlpublic void onClick(View v) { if(v == ButtonName) { ButtonName.setImageResource(R.drawable.ImageName); } }
或者,使用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="@drawable/login_selected" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/login_mouse_over" /> <!-- focused --> <item android:drawable="@drawable/login" /> <!-- default --> </selector>
ButtonName.setBackgroundDrawable(getResources().getDrawable(R.drawable.ImageName));