【问题标题】:Android overflow menu positioned above actionbar/toolbar?Android溢出菜单位于操作栏/工具栏上方?
【发布时间】:2014-12-25 15:39:01
【问题描述】:

我正在尝试让我的溢出菜单显示在我的应用顶部栏下方。当我使用 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>

这确实将材料设计主题应用到我在此布局中的工具栏:

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

但由于某种原因,我在 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);
            }

似乎也找不到遇到这种情况的人。

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

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

【问题讨论】:

    标签: android android-appcompat material-design


    【解决方案1】:

    一些简单的步骤 转到android清单文件并搜索您的下拉菜单所在的活动,对我来说,欢迎活动 现在按下控制并点击主题名称

    <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    

    您将移至编写主题的样式文件 一旦你去那里,你

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    

    现在将这两个项目粘贴到那里

    <item name="overlapAnchor">false</item>
    <item name="android:dropDownVerticalOffset">5.0dp</item>
    

    你已经完成了......

    P.S:不幸的是,我无法在此处分享屏幕截图,似乎在添加图像时出现了一些错误。但是你可以在这个链接上看到 Image File

    【讨论】:

      【解决方案2】:

      如果您在清单中更改 Activity 的主题,则溢出菜单会出现在操作栏下方

      android:theme="@android:style/Theme.Holo.Light"
      

      【讨论】:

        【解决方案3】:

        How I can place overflow menu below toolbar instead of overflow menu to overlaps the app bar 上查看@Girish Kumar 的答案。将溢出菜单移到应用栏底部绝对是可能的。

        【讨论】:

          【解决方案4】:

          根据Material Design specifications(参见菜单部分):

          菜单是一张始终与应用栏重叠的临时纸,而不是作为应用栏的延伸。

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

          【讨论】:

          • 谢谢,但我已经在我的主要活动中这样做了。不是菜单不起作用,只是看起来不对:i.imgur.com/0ybW1A9.png
          • 根据你的截图更新了我的答案:你看到的是一个 Material Design 菜单。
          • 哦,该死的,猜猜我拥有的实现 Material Design 的应用程序给了我错误的想法。谢谢!!
          • @Tania - 许多人尚未迁移到官方 AppCompat 或使用旧版本(此时在 Google 应用程序中最常见),但设计规范和作为其代码实现发布的 AppCompat v21应该被认为是真相的来源。
          • @ianhanniballake 我建议你给刷新一个图标并添加android:showAsAction="ifRoom" app:showAsAction="ifRoom"
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多