Android的溢出菜单位于上方动作条/工具栏? [英] Android overflow menu positioned above actionbar/toolbar?

查看:205
本文介绍了Android的溢出菜单位于上方动作条/工具栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个堆栈溢出问题。裸陪我,如果我不补充足够的或者我加了太多的细节。

我试图让我的溢出菜单下面出现在我的应用程序中的最上面一栏。当我用的是河洛主题,它这样做就好了,但我试图让我的应用程序使用appcompat V7库使用材料设计。

所以,我的主题,现在由AppCompat延伸:

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
    &所述;! - 应用于应用程序或活动的主题 - >
    <样式名称=CustomActionBarTheme
           父=Theme.AppCompat.Light.DarkActionBar>
        <项目名称=colorPrimary> @色/ ucla_blue< /项目>
        <项目名称=colorAccent> @色/ caldroid_white< /项目>
        <项目名称=机器人:actionMenuTextColor> @色/ caldroid_black< /项目>
        <项目名称=机器人:windowNoTitle>真< /项目>
        <项目名称=windowActionBar>假< /项目>
    < /风格>
< /资源>
 

这确实恰当应用材料的设计主题,以我的工具栏在此布局:

 <的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / linearlayout_root_main
    机器人:layout_height =match_parent
    机器人:layout_width =match_parent>
    &所述;! - 甲DrawerLayout旨在被用作使用match_parent为宽度和高度,以消耗可使用的全部空间的顶层内容图。 - >
    < android.support.v4.widget.DrawerLayout
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:工具=htt​​p://schemas.android.com/tool​​s
        机器人:ID =@ + ID / drawer_layout
        机器人:layout_height =match_parent
        机器人:layout_width =match_parent
        工具:上下文=com.ucla.ieee.app.MainActivity>

        <的LinearLayout
            机器人:layout_height =match_parent
            机器人:layout_width =match_parent
            机器人:方向=垂直>

            < android.support.v7.widget.Toolbar
                的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
                机器人:ATTR / colorPrimary后台=
                机器人:ID =@ + ID /工具栏
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_width =FILL_PARENT
                机器人:=了minHeight?ATTR / actionBarSize
                应用程序:popupTheme =@风格/ ThemeOverlay.AppCompat.Light
                应用程序:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar/>
            <! - 为主要内容的视图,该视图下方消耗整个
             使用match_parent在两个方向的可用空间。 - >
            <的FrameLayout
                机器人:ID =@ + ID /容器
                机器人:layout_height =match_parent
                机器人:layout_width =match_parent/>
        < / LinearLayout中>


        <! - 导航抽屉 - >
        <片段
            机器人:ID =@ + ID / navigation_drawer
            机器人:layout_gravity =开始
            机器人:layout_height =match_parent
            机器人:layout_marginTop =?ATTR / actionBarSize
            机器人:layout_width =@扪/ navigation_drawer_width
            机器人:名称=navigation.NavigationDrawerFragment
            工具:布局=@布局/ fragment_navigation_drawer/>

    < /android.support.v4.widget.DrawerLayout>
< / LinearLayout中>
 

但由于某些原因,菜单我在充气出现onCreateOptionsMenu在错误的地方。显然,我不能发表图片,但基本上溢出菜单覆盖,而不是仅仅出现在工具栏下面的工具栏。这开始发生时,我使用Theme.AppCompat首先在一个正常的动作条​​甚至现在有一个工具栏。

 工具栏=(栏)findViewById(R.id.toolbar);
            开关(选择){
                案例FRONT_PAGE:
                    toolbar.inflateMenu(R.menu.main_settings);
                    返回false;
                情况公告:
                    toolbar.inflateMenu(R.menu.refresh_settings);
                    返回false;
                案例会员:
                    toolbar.inflateMenu(R.menu.edit_member);
                    返回false;
                案例日历:
                    toolbar.inflateMenu(R.menu.refresh_settings);
                    返回false;
                案例POINTS_REWARDS:
                    toolbar.inflateMenu(R.menu.main_settings);
                    返回false;
                案例帮助:
                    toolbar.inflateMenu(R.menu.main_settings);
                    返回false;
                默认:
                    toolbar.inflateMenu(R.menu.main_settings);
            }
 

似乎无法找到任何人遇到这个问题,以及,我希望有人能帮助我弄清楚了这一点!谢谢!

下面是我的菜单之一的例子:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    工具:上下文=com.ucla_ieee.app.MainActivity>
    <项目机器人:ID =@ + ID / action_refresh
          机器人:标题=@字符串/ action_refresh
          应用程序:showAsAction =从不/>
< /菜单>
 

解决方案

占的材料设计规范(请参阅菜单部分):

  

一个菜单是一张临时表总是重叠的应用栏,而不是表现为应用栏的扩展。

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

This is my first stack overflow question. Bare with me if I don't add enough or I add too much detail.

I'm trying to get my overflow menu to appear below the top bar in my app. When I was using the Holo theme it did this just fine, but I'm trying to get my app to use material design using the appcompat v7 library.

So my theme now extends from 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>

And this properly does apply the material design theme to my toolbar in this layout:

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

But for some reason, the menu I inflate in onCreateOptionsMenu appears in the wrong place. Apparently I can't post images but basically the overflow menu covers the toolbar instead of appearing just below the toolbar. This started happening when I use "Theme.AppCompat" first in a normal ActionBar and even now with a Toolbar.

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);
            }

Couldn't seem to find anyone experiencing this as well, I hope someone can help me figure this out! Thanks!

Here's an example of one of my menus:

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

解决方案

Per the Material Design specifications (see the Menus section):

A menu is a temporary sheet of paper that always overlaps the app bar, rather than behaving as an extension of the app bar.

So what you are seeing is the correct Material design for menus.

这篇关于Android的溢出菜单位于上方动作条/工具栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆