How to set menu to Toolbar in Android

后端 未结 14 1399
轻奢々
轻奢々 2020-11-29 20:17

I want use ToolBar instead of ActionBar, but don\'t show me menu in toolbar!!! i want set menu such as Refresh or Setting<

相关标签:
14条回答
  • 2020-11-29 20:33

    You need to override this code in your Activity:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu, this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main2, menu);
        return true;
    }
    

    and set your toolbar like this:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    
    0 讨论(0)
  • 2020-11-29 20:36

    You can still use the answer provided using Toolbar.inflateMenu even while using setSupportActionBar(toolbar).

    I had a scenario where I had to move toolbar setup functionality into a separate class outside of activity which didn't by-itself know of the event onCreateOptionsMenu.

    So, to implement this, all I had to do was wait for Toolbar to be drawn before calling inflateMenu by doing the following:

    toolbar.post {
        toolbar.inflateMenu(R.menu.my_menu)
    }
    

    Might not be considered very clean but still gets the job done.

    0 讨论(0)
  • 2020-11-29 20:36

    You can achieve this by two methods

    1. Using XML
    2. Using java

    Using XML Add this attribute to toolbar XML app:menu = "menu_name"

    Using java By overriding onCreateOptionMenu(Menu menu)

    public class MainActivity extends AppCompatActivity { 
    
      private Toolbar toolbar;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    }
    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.demo_menu,menu);
            return super.onCreateOptionsMenu(menu);
        }
    }
    

    for more details or implementating click on the menu go through this article https://bedevelopers.tech/android-toolbar-implementation-using-android-studio/

    0 讨论(0)
  • 2020-11-29 20:37

    Don't use setSupportActionBar(toolbar)

    I don't know why but this works for me.

    toolbar = (Toolbar) findViewById(R.id.main_toolbar);
    toolbar.setSubtitle("Test Subtitle");
    toolbar.inflateMenu(R.menu.main_manu);
    

    For menu item click do this

    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
    
            @Override
            public boolean onMenuItemClick(MenuItem item) {
    
                if(item.getItemId()==R.id.item1)
                {
                    // do something
                }
                else if(item.getItemId()== R.id.filter)
                {
                   // do something
                }
                else{
                   // do something
                }
    
                return false;
            }
        });
    

    Will update the 'why' part of this answer when I find a proper explanation.
    Happy to help anyway :) Peace.

    0 讨论(0)
  • 2020-11-29 20:37

    Also you need this, to implement some action to every options of menu.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_help:
                Toast.makeText(this, "This is teh option help", Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
        return true;
    }
    
    0 讨论(0)
  • 2020-11-29 20:39

    In my case, I'm using an AppBarLayout with a CollapsingToolbarLayout and the menu was always being scrolled out of the screen, I solved my problem by switching android:actionLayout in menu's XML to the toolbar's id. I hope it can help people in the same situation!

    activity_main.xml

    <android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:fab="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activities.MainScreenActivity"
        android:screenOrientation="portrait">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            app:elevation="0dp"
            android:theme="@style/AppTheme.AppBarOverlay">
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsingBar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_scrollFlags="exitUntilCollapsed|scroll"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginStart="48dp"
                app:expandedTitleMarginEnd="48dp"
                >
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:elevation="0dp"
                    app:popupTheme="@style/AppTheme.PopupOverlay"
                    app:layout_collapseMode="pin"/>
            </android.support.design.widget.CollapsingToolbarLayout>
    
    
        </android.support.design.widget.AppBarLayout>
    </android.support.design.widget.CoordinatorLayout>
    

    main_menu.xml

    <?xml version="1.0" encoding="utf-8"?> <menu
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/logoutMenu"
            android:orderInCategory="100"
            android:title="@string/log_out"
            app:showAsAction="never"
            android:actionLayout="@id/toolbar"/>
        <item
            android:id="@+id/sortMenu"
            android:orderInCategory="100"
            android:title="@string/sort"
            app:showAsAction="never"/> </menu>
    
    0 讨论(0)
提交回复
热议问题