我目前在弹出菜单中使用此样式:

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


如何使弹出菜单的顶部和底部有多余的空间:

android - 自定义弹出菜单样式-LMLPHP

我怎么做,所以没有多余的空间?是否有某种样式可以包装内容?

编辑:我忘了提及我以编程方式给视图充气,因此可能是一个问题

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


popup_menu的XML:

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

最佳答案

对于“溢出菜单”,您可以在应用程序主题中定义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>

android - 自定义弹出菜单样式-LMLPHP
对于弹出窗口,您可以使用:
<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>

关于android - 自定义弹出菜单样式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58844707/

10-08 23:25