使用工具栏作为操作栏不会使菜单膨胀

Giu*_*ppe 4 android android-support-library android-toolbar

我正在尝试使用工具栏作为操作栏,我遵循Chris Banes指南:

https://chris.banes.me/2014/10/17/appcompat-v21/

现在遵循该设置我有一个空的工具栏,似乎getMenuInflater().inflate()不起作用.

空工具栏

在我的活动中,我:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menuhome, menu);
        [...]
Run Code Online (Sandbox Code Playgroud)

并在onCreate()中:

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
Run Code Online (Sandbox Code Playgroud)

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:allmytv="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/linearlayoutmain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v7.widget.Toolbar
        xmlns:allmytv="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary" />

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        allmytv:pstsIndicatorColor="#f57c1d" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </android.support.v4.view.ViewPager>


</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

哪里我错了?

kun*_*l.c 5

要将工具栏用作操作栏,您需要将属性windowActionBar设置为false.

在styles.xml中包含以下内容

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item> 
        <item name="colorPrimary">#4285F6</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

在标签下的manifest.xml中使用上面的主题

android:theme="@style/AppCompatTheme"
Run Code Online (Sandbox Code Playgroud)

我用工具栏来夸大菜单:

 toolbar.inflateMenu(R.menu.your_toolbar_menu);
Run Code Online (Sandbox Code Playgroud)