How to add dividers between specific menu items?

后端 未结 7 1919
旧巷少年郎
旧巷少年郎 2020-12-03 01:35

Background

I have a menu item in the action bar (toolbar actually) that when clicked, shows a list of items to choose from, similar to radio-buttons:



        
7条回答
  •  时光说笑
    2020-12-03 02:06

    I did it this way:

    Reference screenshot:

    style.xml:

        
    
     
        
    

    Java code:

    private void showPopup(View v) {
            Context wrapper = new ContextThemeWrapper(this, R.style.popup);
            PopupMenu mypopupmenu = new PopupMenu(wrapper, v);
            MenuInflater inflater = mypopupmenu.getMenuInflater();
            inflater.inflate(R.menu.menu_patient_language, mypopupmenu.getMenu());
            mypopupmenu.show();
            mypopupmenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    txtPreferredLanguage.setText(item.getTitle().toString());
                    switch (item.getItemId()) {
                        case R.id.menuEnglish:
                            // Your code goes here
                            break;
    
                        case R.id.menuFrench:
                            // Your code goes here
                            break;
                    }
                    return false;
                }
            });
        }
    

    Hope this will help you.

提交回复
热议问题