Android MenuItem Toggle Button

孤街浪徒 提交于 2019-11-26 21:21:10

问题


In my Android application, I want a setting to be toggleable in it's menu like the Dialer app does for speaker and mute. You can see a picture below:

http://www.isaacwaller.com/images/acall.png

You see how the Speaker, Mute and Hold options are toggle buttons - you can tap them again and they will toggle the green color. They may do this in a custom way, but I suspect it is a option (I tried setting the Checkable attribute).


回答1:


It's looks like this menu item is implemented as a custom view.

In the android source code you can take a look at com.android.phone.InCallMenuView.java to see how this is implemented.

It doesn't look like it is part of the public API, but it looks pretty self-contained. If your project has a compatible license, you may be able to copy it into your project and use and modify it as you see fit.




回答2:


You could do something like the snippet below as well, originally sourced from anddev.org

public boolean onPrepareOptionsMenu(final Menu menu) {       
      if(super.mMapView.isTraffic()) 
           menu.findItem(MENU_TRAFFIC_ID).setIcon(R.drawable.traffic_off_48); 
      else 
           menu.findItem(MENU_TRAFFIC_ID).setIcon(R.drawable.traffic_on_48); 

      return super.onPrepareOptionsMenu(menu); 
 }


来源:https://stackoverflow.com/questions/568533/android-menuitem-toggle-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!