Showing a menu overflow button in a Toolbar

后端 未结 2 780
遇见更好的自我
遇见更好的自我 2021-01-18 14:00

I\'ve created a Toolbar for Lollipop, but I can\'t seem to figure out how to add an overflow button to the toolbar. I\'m not going to be using the v7 appcompat

相关标签:
2条回答
  • 2021-01-18 14:11

    If you want to add the item in the overflow menu you should set never as android:showAsAction. If you set always the item will be an icon in the toolbar.

    Edit your menu.xml like this:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <item
            android:id="@+id/action_settings"
            android:orderInCategory="100"
            android:title="@string/action_settings"
            android:showAsAction="never"
            android:visible="true" />
    
    </menu>
    

    Also you need to call setActionBar(). Edit initToolbar() like this:

    private void initToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitleTextColor(Color.WHITE);
        mToolbar.setTitle("gnappo");
        mToolbar.showOverflowMenu();
        setActionBar(mToolbar);
    }
    
    0 讨论(0)
  • 2021-01-18 14:38

    If your toolbar is of the package android.support.v7.widget, use setSupportActionBar(mToolbar); instead of setActionBar(mToolbar);

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