android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现
android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现首先看到selector的属性:
android:state_focused
android:state_pressed
android:state_selected
android:state_enabled
它们之间的执行是有执行顺序的写xml的时候特别要分析好执行顺序,否则达不到要实现的效果:
现在实现效果如下:
当点击停止按钮时,
有点击效果,和选中效果。
具体代码如下:
暂停:
<?xml version="1.0" encoding="utf-8"?>
<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>
停止;
<?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/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>
<?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/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>
布局中:
<ImageButton
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" />
代码中:
public void onClick(View arg0) {
// 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;
补充:移动开发 , Android ,