Android: Custom view for a menu item

2019-02-12 18:19发布

问题:

I want to be able to specify a custom view for a MenuItem.

I want to display an indicator on a shopping cart menu item in the action bar displaying how many items are in the cart currently.

Is the only way that I can do this is through adding a custom view instead of utilising the MenuItem lifecycle etc.?

回答1:

To use a custom view on a MenuItem call setActionView(View view).

You can supply either an already inflated view or a layout resource ID.