如何在Android中将菜单设置为工具栏

我想使用ToolBar而不是ActionBar ,但不要在工具栏中显示我的菜单! 我想在ActionBar设置菜单,如刷新设置button。
请为我的问题图片显示此链接: 图片链接

Toolbar.xml代码:

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" app:navigationContentDescription="@string/abc_action_bar_up_description" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:title="Main Page" android:gravity="center"/> 

MainPage.java代码:

 public class MainPage extends AppCompatActivity { private Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_page); toolbar = (Toolbar) findViewById(R.id.main_toolbar); setSupportActionBar(toolbar); if (getSupportActionBar() != null) { getSupportActionBar().setTitle("Main Page"); } toolbar.setSubtitle("Test Subtitle"); toolbar.inflateMenu(R.menu.main_manu); } } 

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/menu_main_setting" android:icon="@drawable/ic_settings" android:orderInCategory="100" app:showAsAction="always" android:actionLayout="@layout/toolbar" android:title="Setting" /> <item android:id="@+id/menu_main_setting2" android:icon="@drawable/ic_settings" android:orderInCategory="200" app:showAsAction="always" android:actionLayout="@layout/toolbar" android:title="Setting" /> </menu> 

如何解决这个问题,并在Toolbar显示菜单? 感谢所有亲爱的<3

Solutions Collecting From Web of "如何在Android中将菜单设置为工具栏"

只需在MainPage.java中重写onCreateOptionsMenu

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

你需要在你的活动中重写这段代码:

  @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; } 

并像这样设置你的工具栏:

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);