Is it possible to Implement Toggle Button in Action Menu Item using Actionbar sherlock in android

后端 未结 4 1887
栀梦
栀梦 2021-02-04 02:30

I have an app, which have toggle button in action menu item, though i\'m using Actionbar Sherlock, I don\'t know, how to place the toggle button in the action menu item. I don\'

4条回答
  •  失恋的感觉
    2021-02-04 03:18

    Other approach would be to use a custom layout for your ActionBar:

    Basically you define a layout that contains your Toggle:

    
      
    
    

    ALTERNATIVE 1: Then in your Activity or Fragment container you do:

    ActionBar actionBar = getSupportActionBar();
    actionBar.setCustomView(R.layout.actionbar_top);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
    ...
    ToggleButton button = (ToggleButton) findViewById(R.id.actionbar_service_toggle);
    

    Notice that you are having a real ToggleButton and you are handling it in code as a real object ToggleButton, which has lots of advantages compared to having you re-implement your own toggle (theme, reliability, views hierarchy, native support...).

    Source code here.


    ALTERNATIVE 2: Another way to do it is embed your custom view into a regular menu view:

    
           
    
    

提交回复
热议问题