How to add dividers between specific menu items?

后端 未结 7 1884
旧巷少年郎
旧巷少年郎 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:

        <style name="popup" parent="Widget.AppCompat.ListView.DropDown">
                <item name="android:divider">@color/colorPrimary</item>
                <item name="android:dividerHeight">1dp</item>
                <item name="android:textColor">@color/colorPrimary</item>
                <item name="android:itemBackground">@android:color/white</item>
        </style>
    
     <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    
            <!--- Customize popmenu -->
            <item name="android:dropDownListViewStyle">@style/popup</item>
    
    
        </style>
    

    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.

    0 讨论(0)
提交回复
热议问题