Android溢出菜单位于操作栏/工具栏上方?

Tan*_*nia 14 android android-appcompat material-design

我正在尝试将溢出菜单显示在我的应用中的顶部栏下方.当我使用Holo主题时,它做得很好,但我正在尝试使用appcompat v7库让我的应用程序使用材料设计.

所以我的主题现在扩展到AppCompat:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/ucla_blue</item>
        <item name="colorAccent">@color/caldroid_white</item>
        <item name="android:actionMenuTextColor">@color/caldroid_black</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

这恰当地将材质设计主题应用于此布局中的工具栏:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearlayout_root_main"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        tools:context="com.ucla.ieee.app.MainActivity">

        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="vertical">

            <android.support.v7.widget.Toolbar
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:background="?attr/colorPrimary"
                android:id="@+id/toolbar"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:minHeight="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
            <!-- As the main content view, the view below consumes the entire
             space available using match_parent in both dimensions. -->
            <FrameLayout
                android:id="@+id/container"
                android:layout_height="match_parent"
                android:layout_width="match_parent"/>
        </LinearLayout>


        <!-- The navigation drawer -->
        <fragment
            android:id="@+id/navigation_drawer"
            android:layout_gravity="start"
            android:layout_height="match_parent"
            android:layout_marginTop="?attr/actionBarSize"
            android:layout_width="@dimen/navigation_drawer_width"
            android:name="navigation.NavigationDrawerFragment"
            tools:layout="@layout/fragment_navigation_drawer"/>

    </android.support.v4.widget.DrawerLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,我在onCreateOptionsMenu中膨胀的菜单出现在错误的位置.显然我不能发布图像,但基本上溢出菜单覆盖工具栏而不是出现在工具栏下方.当我在普通的ActionBar中使用"Theme.AppCompat"时,甚至现在使用工具栏时,这种情况就开始发生了.

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            switch (selected) {
                case FRONT_PAGE:
                    toolbar.inflateMenu(R.menu.main_settings);
                    return false;
                case ANNOUNCEMENTS:
                    toolbar.inflateMenu(R.menu.refresh_settings);
                    return false;
                case MEMBERSHIP:
                    toolbar.inflateMenu(R.menu.edit_member);
                    return false;
                case CALENDAR:
                    toolbar.inflateMenu(R.menu.refresh_settings);
                    return false;
                case POINTS_REWARDS:
                    toolbar.inflateMenu(R.menu.main_settings);
                    return false;
                case HELP:
                    toolbar.inflateMenu(R.menu.main_settings);
                    return false;
                default:
                    toolbar.inflateMenu(R.menu.main_settings);
            }
Run Code Online (Sandbox Code Playgroud)

似乎没有找到任何经历过这种情况的人.

这是我的一个菜单的示例:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.ucla_ieee.app.MainActivity">
    <item android:id="@+id/action_refresh"
          android:title="@string/action_refresh"
          app:showAsAction="never" />
</menu>
Run Code Online (Sandbox Code Playgroud)

ian*_*ake 20

根据材料设计规范(参见菜单部分):

菜单是一张临时的纸张,总是与应用栏重叠,而不是作为应用栏的扩展.

菜单重叠应用栏

所以你看到的是菜单的正确材料设计.

  • @ianhanniballake我建议你刷新一个图标并添加`android:showAsAction ="ifRoom"app:showAsAction ="ifRoom"` (3认同)