android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现
首先看到selector的属性:
android:state_focused
android:state_pressed
android:state_selected
android:state_enabled
它们之间的执行是 有执行顺序的写xml的时候特别要分析好执行顺序,否则达不到要实现的效果:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/icon_pause_active" android:state_pressed="true"/>
<item android:drawable="@drawable/icon_pause_active" android:state_selected="true"/>
<item android:drawable="@drawable/icon_pause_inactive" android:state_enabled="true"/>
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/icon_stop_active"/>
<item android:state_selected="true" android:drawable="@drawable/icon_stop_active"/>
<item android:state_enabled="true" android:drawable="@drawable/icon_stop_inactive"/>
播放:
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/icon_play_active"/>
<item android:state_selected="true" android:drawable="@drawable/icon_play_active"/>
<item android:state_enabled="true" android:drawable="@drawable/icon_play_inactive"/>
</selector>
android:layout_marginRight="15dp"
android:background="@null"
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="
@drawable/music_stop_style" />
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:src="
@drawable/music_play_style" />
<ImageButton
android:layout_marginLeft="15dp"
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@
drawable/music_pause_style" />
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.stop:
stop.setSelected(true);
play.setSelected(false);
pause.setSelected(false);
break;
case R.id.play:
stop.setSelected(false);
play.setSelected(true);
pause.setSelected(false);
break;
case R.id.pause:
stop.setSelected(false);
play.setSelected(false);
pause.setSelected(true);
break;