自定义弹出菜单样式 [英] Customizing popup menu style

查看:229
本文介绍了自定义弹出菜单样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently using this style for my popup menu:

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

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

How ever it causes popup menus to have extra space on the top and bottom:

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

How do I make it so there is no extra space? Is there a certain style so that it wraps its contents?

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

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 for 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 属性.

For the OverflowMenu you can define in your app theme the actionOverflowMenuStyle attribute.

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

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

For a popup you can use:

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

这篇关于自定义弹出菜单样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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