本例使用selector实现点击切换图片效果
public class MainActivity extends Activity {
private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.bt);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.expandablelist_test.MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/bt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/imgselector"
/>
</LinearLayout>
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/a"/>
<item android:state_focused="true"
android:drawable="@drawable/c"/>
<item android:drawable="@drawable/b"/>
</selector>