如何将溢出菜单放置在工具栏下方而不是溢出菜单与工具栏重叠 [英] How I can place overflow menu below toolbar instead of overflow menu to overlaps the tool bar

查看:100
本文介绍了如何将溢出菜单放置在工具栏下方而不是溢出菜单与工具栏重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为工具栏添加了shareActionProvider,我的问题是,一旦我点击共享图标,弹出菜单将覆盖工具栏内容,但是当我点击弹出菜单下列出的查看全部选项时,菜单,弹出菜单将完全位于工具栏下方.

I have added the shareActionProvider for the tool bar my problem is as soon as i tap on share icon the popup menu will cover the toolbar content but when i tap on see all option listed under the popup menu , the popup menu will perfectly settle below the tool bar.

我的要求是,当我点击共享"图标时,我的弹出菜单应立即填充在工具栏下方,而不是在我选择"查看全部"选项之后.

My requirement is as soon as i tap on share icon my popup menu should be populated below the tool bar not after i select the see all option.

menu.xml

<item
    android:id="@+id/menu_item_share"
    app:showAsAction="always"
    android:title="Share"
    android:icon="@drawable/abc_ic_menu_share_mtrl_alpha"/>

Java代码

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuItem item = menu.findItem(R.id.menu_item_share);

    mShareActionProvider = new ShareActionProvider(getActivity()) {
        @Override
        public View onCreateActionView() {
            return null;
        }
    };

    MenuItemCompat.setActionProvider(item, mShareActionProvider);

    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(createShareIntent());
    }
}

style.xml

style.xml

<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
    <item name="actionOverflowMenuStyle">@style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
    <item name="android:popupBackground">@color/colorPrimary</item>

    <!-- Required for pre-Lollipop. -->
    <item name="overlapAnchor">false</item>
    <item name="android:dropDownVerticalOffset">-4.0dip</item>

    <!-- Required for Lollipop. -->
    <item name="android:overlapAnchor">false</item>
    <item name="android:dropDownVerticalOffset">-4.0dip</item>
</style>

推荐答案

根据

According to 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.

要更改其主要样式,请使用<item name="actionOverflowMenuStyle">@style/OverflowMenu</item>,其中

To change it in your main style use <item name="actionOverflowMenuStyle">@style/OverflowMenu</item>, where

<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
  <!-- Required for pre-Lollipop. -->
   <item name="overlapAnchor">false</item>
   <item name="android:dropDownVerticalOffset">-4.0dip</item>
  <!-- Required for Lollipop. -->
   <item name="android:overlapAnchor">false</item>
   <item name="android:dropDownVerticalOffset">4.0dip</item>
</style>

对于棒棒糖样式,必须为values-v21.

For Lollipop style must be in values-v21.

这篇关于如何将溢出菜单放置在工具栏下方而不是溢出菜单与工具栏重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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