果冻豆通知中的双状态动作按钮

时间:2013-08-07 11:10:49

标签: android notifications drawable statelistdrawable

我正在尝试将媒体播放器控件实现到我的通知中。我需要播放/暂停按钮在“播放”可绘制和动态“暂停”绘制之间切换(基本上在用户的触摸上)。例如,用户触摸“暂停”按钮后,需要更改为“播放”。当用户触摸“播放”按钮时,需要更改回“暂停”。

我假设最好的方法是创建一个StateListDrawable XML并将其设置为action按钮的drawable。不幸的是,我似乎无法使StateListDrawable工作。这是我的drawable XML文件:

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android" >  
    <item android:state_first="true"  
          android:drawable="@drawable/pause_track_notification" />  
    <item android:state_last="true"
          android:drawable="@drawable/play_track_notification" />  
</selector> 

我错过了XML中的重要内容吗?现在,我只是得到一个空白区域,其中应该显示播放/暂停按钮。我如何让它工作?谢谢!

1 个答案:

答案 0 :(得分:1)

将此用作切换按钮和以下选择器:

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android" >  
    <item android:state_checked="true" 
          android:drawable="@drawable/pause_track_notification" />  
    <item android:state_checked="false"
          android:drawable="@drawable/play_track_notification" />
    <item android:drawable="@drawable/play_track_notification" /> <!-- the default -->
</selector>