gpt4 book ai didi

android - 自定义弹出菜单样式

转载 作者:行者123 更新时间:2023-11-29 16:28:08 25 4
gpt4 key购买 nike

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

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

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

enter image description here

如何制作才不会有多余的空间?是否有某种样式可以包装其内容?

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

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:

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

enter image description here

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com