自定义弹出菜单样式

Customizing popup menu style

我目前正在为我的弹出菜单使用这种样式:

<style name="mainActivityTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/colorBlack</item>
    <item name="colorPrimaryDark">@color/colorBlack</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

它如何导致弹出菜单在顶部和底部有额外的 space:

我怎样才能做到没有多余的space?是否有某种样式可以包裹其内容?

编辑:我忘了提到我以编程方式膨胀视图,所以这可能是个问题

private void inflateMoreMenu(View view){
        PopupMenu popupMenu = new PopupMenu(mContext, view);
        popupMenu.inflate(R.menu.popup_menu);
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                switch (item.getItemId()){
                    case R.id.popup_menu_report:
                        //Inflate a layout

                        break;
                    case R.id.popup_menu_block:
                        //Inflate a layout

                        break;

                }
                return false;
            }
        });

        @SuppressLint("RestrictedApi")
        MenuPopupHelper menuHelper = new MenuPopupHelper(mContext, (MenuBuilder) popupMenu.getMenu(), view);
        menuHelper.setForceShowIcon(true);
        menuHelper.show();
    }

XML 对于 popup_menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/popup_menu_report"
        android:title="Report"
        android:icon="@drawable/icon_messages_report"/>

    <item
        android:id="@+id/popup_menu_block"
        android:title="Block"
        android:icon="@drawable/icon_alertdialog_block"/>

</menu>

对于 OverflowMenu,您可以在应用主题中定义 actionOverflowMenuStyle 属性。

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
   <item name="actionOverflowMenuStyle">@style/popupOverflowMenu</item>
</style>

有:

  <style name="popupOverflowMenu" parent="@style/Widget.MaterialComponents.PopupMenu.Overflow">
    <item name="android:popupBackground">@drawable/my_mtrl_popupmenu_background</item>   
  </style>

drawable/my_mtrl_popupmenu_background.xml 文件:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="?attr/colorSurface"/>

  <corners
      android:bottomLeftRadius="4dp"
      android:bottomRightRadius="4dp"
      android:topLeftRadius="4dp"
      android:topRightRadius="4dp"/>

</shape>

原始文件有 padding:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="?attr/colorSurface"/>

  <corners
    ..../>

  <padding
      android:bottom="8dp"
      android:top="8dp"/>

</shape>

对于弹出窗口,您可以使用:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
   <item name="popupMenuStyle">@style/popupMenu</item>
</style>

<style name="popupMenu" parent="@style/Widget.MaterialComponents.PopupMenu">
    <item name="android:popupBackground">@drawable/my_mtrl_popupmenu_background</item>   
</style>