android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现
16lz
2021-12-04
android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现
首先看到selector的属性:
android:state_focusedandroid:state_pressedandroid:state_selectedandroid: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(安卓)Trick 7: 把TextView中的文字添加阴影效果及Style
- [APK破解]终极狂飙3D漂移,无限金钱,解锁全部关卡
- Android(安卓)Trick 7: 把TextView中的文字添加阴影效果及Style
- Android—最新版Android(安卓)studioSDK下载与配置—2020-7-13
- Android的dumpsys命令
- Android实现九宫格 主界面应用列表效果
- Android单元测试初探——Instrumentation
- [转]android单元测试初探——Instrumentation
- android 按钮点击的两种方法以及长按事件