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

时间:2014-11-12 19:14:36

标签: android android-support-library android-toolbar

我试图将工具栏用作操作栏,然后按照Chris Banes指南进行操作:

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

现在按照设置我有一个空的工具栏,似乎getMenuInflater()。inflate()不起作用。

empty Toolbar

在我的活动中我

@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);
        [...]

并在onCreate()中:

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

这是我的布局:

<?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>

我哪里错了?

2 个答案:

答案 0 :(得分: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>

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

android:theme="@style/AppCompatTheme"

我使用工具栏来夸大菜单:

 toolbar.inflateMenu(R.menu.your_toolbar_menu);

答案 1 :(得分:2)

这绝对是一个非常具体的问题。 问题是我的启动画面设置为:

setContentView(R.layout.splashhome);

工具栏为空,因为我在启动画面激活时调用了setSupportActionBar(工具栏)。

将setSupportActionBar(工具栏)移动到正确的位置修复它!

相关问题