我已经搜索并找到了其他同样的问题,但是仍然无法解决操作栏中未显示的菜单项的问题。我将发布操作条形码,menu_main.xml和MainActivity

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/ColorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:elevation="10dp"
        >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PROJECT TIMELINE"
        android:id="@+id/toolbar_title"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="@color/ColorPrimaryDark" />

</android.support.v7.widget.Toolbar>


这是来自menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">


    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item android:id="@+id/search"
        android:title="@string/Search"
        android:icon="@drawable/ic_action"
        app:showAsAction="ifRoom"
        />

</menu>


我在MainActivity.java中写的

public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

最佳答案

初始化工具栏,然后在Activity的onCreate中调用此方法

setSupportActionBar(yourToolbar);

10-07 20:54