Android Checkable Menu Item

后端 未结 9 1795
悲哀的现实
悲哀的现实 2020-12-01 00:59

I have the following menu layout in my Android app:




        
9条回答
  •  失恋的感觉
    2020-12-01 01:41

    You can create a checkable menu item by setting the actionViewClass to a checkable widget like android.widget.CheckBox

    res/menu/menu_with_checkable_menu_item.xml

    
        
    
    

    And you can can even style it to be a checkable star if you set actionLayout to a layout with a styled android.widget.CheckBox

    res/layout /action_layout_styled_checkbox.xml

    
    

    res/menu/menu_with_checkable_star_menu_item.xml

    
        
    
    

    To set the value

    menuItem.setChecked(true/false);
    

    To get the value

    menuItem.isChecked()
    

    Cast MenuItem to CheckBox

    CheckBox checkBox= (CheckBox) menuItem.getActionView();
    

提交回复
热议问题