Android : Get view Reference to a Menu Item

前端 未结 6 722
醉酒成梦
醉酒成梦 2020-11-27 05:19

I plan to use quick actions UI pattern in my application. Android Quick Actions UI Pattern . The quick action window needs a pivot view to stick to.

    qui         


        
6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 05:46

    An update for anyone that want to find the menu view item for other reasons (like I wanted).

    If you have access to and use AppCompat's Toolbar there is a way. It's not the most efficient way, but it's the easiest way I've found to access the menu item's view.

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
    
        // Find Menu
        for (int toolbarChildIndex = 0; toolbarChildIndex < toolbar.getChildCount(); toolbarChildIndex++) {
            View view = toolbar.getChildAt(toolbarChildIndex);
    
            // Found Menu
            if (view instanceof ActionMenuView) {
                ActionMenuView menuView = (ActionMenuView) view;
    
                // All menu items
                for (int menuChildIndex = 0; menuChildIndex < menuView.getChildCount(); menuChildIndex++) {
                    ActionMenuItemView itemView = (ActionMenuItemView) menuView.getChildAt(menuChildIndex);
                    // Do something to itemView...
                }
            }
        }
    }
    

提交回复
热议问题