How I can place overflow menu below toolbar instea

2019-06-20 22:15发布

问题:

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 code

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

回答1:

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.


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>

For Lollipop style must be in values-v21.