Showing Custom Layout on Overflow Drop Down Menu Item? Android

前端 未结 2 1839
遥遥无期
遥遥无期 2020-12-10 14:07

I am trying to figure out how to show a custom view for items inside the overflow menu (on right of actionbar) but I can\'t seem to figure it out. It still shows the title o

相关标签:
2条回答
  • 2020-12-10 14:35

    I was just facing the same problem on how to list custom rows in overflow menu. Unfortunately there isn't much on web about customising action bars. If you want to list items with just icon and title in over flow menu, you need to do nesting of items within items. I did it like this:

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item
            android:id="@+id/action_settings"
            android:icon="@drawable/ic_action_overflow"
            android:orderInCategory="100"
            android:showAsAction="always">
            <menu>
                <item
                    android:id="@+id/add_source"
                    android:icon="@drawable/add_on"
                    android:orderInCategory="100"
                    android:showAsAction="never"
                    android:title="@string/add_source"/>
                <item
                    android:id="@+id/channel_setup"
                    android:icon="@drawable/channelsetup_on"
                    android:orderInCategory="100"
                    android:showAsAction="never"
                    android:title="@string/channel_setup"/>
            </menu>
        </item>
    
        <item
            android:id="@+id/time"
            android:orderInCategory="99"
            android:showAsAction="always"
            android:title="@string/time_title"/>
        <item
            android:id="@+id/weather"
            android:icon="@drawable/ic_action_cloud"
            android:orderInCategory="98"
            android:showAsAction="always"
            android:title="@string/weather_title"/>
        <item
            android:id="@+id/search"
            android:actionViewClass="android.widget.SearchView"
            android:icon="@drawable/ic_action_search"
            android:orderInCategory="97"
            android:showAsAction="collapseActionView|always"
            android:title="@string/search_title"/>
    
    </menu>
    

    to achieve this thing:

    enter image description here

    And here is the reference to my question:

    link

    I hope it might help you.

    0 讨论(0)
  • 2020-12-10 14:36

    If you just want add a drawable (red dot) after text, you can use imageSpan to add drawable after main text like below:

    private MenuItem settingsItem;
        ....
        SpannableStringBuilder builder = new SpannableStringBuilder(getString(R.string.settings_menu_name) + " .");
        Drawable drawable = getResources().getDrawable(R.drawable.red_dot);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        builder.setSpan(new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE), builder.length()-1, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        settingsItem.setTitle(builder);
    
    0 讨论(0)
提交回复
热议问题