我试图使一个圆角和自定义字体弹出菜单。我正在使用material.io中的材质菜单。我想做这样的事情:
到目前为止,我尝试使用popupmenu,并使用自定义背景绘图添加样式参数:
看到这个问题的答案,我使用了contextthemewrapper,这样我的样式才真正生效。如果不这样做,样式更改将无法工作
private fun showAddGoalsMenu(view: View) {
val wrapper = ContextThemeWrapper(context, R.style.PopupMenu)
val popupMenu = PopupMenu(wrapper, view)
popupMenu.inflate(R.menu.menu_add_goals)
...
}
从material.io菜单主题示例中,我尝试设置属性 popupMenuBackground
在主应用程序样式中,也尝试了设置 android:popupBackground
以个人风格。但背景并不适用:
从其他答案(问题1、问题2、问题3)中,我发现了其他属性,如 background
, android:background
, itemBackground
, android:itemBackground
. 由此,, background
及 android:background
只适合设置颜色。如果我将自定义绘图设置为“背景”,则也会将项目弄乱:itemBackground
及 android:itemBackground
仅为单个项目设置背景。因此,如果我应用圆形背景,结果如下所示:
我的样式代码当前看起来像这样。根据我到目前为止所读到的,这应该是可行的。但是唉。我已经把我做的所有实验都注解掉了。
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorSecondary">@color/colorSecondary</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="bottomNavigationStyle">@style/Widget.App.BottomNavigationView</item>
<!--<item name="textAppearanceLargePopupMenu">@style/TextAppearance.App.Subtitle1</item>-->
<!--<item name="textAppearanceSmallPopupMenu">@style/TextAppearance.App.Subtitle1</item>-->
<!--<item name="popupMenuBackground">@drawable/custom_popup_background</item>-->
<item name="popupMenuStyle">@style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="Widget.MaterialComponents.PopupMenu">
<item name="android:dropDownHorizontalOffset">-4dp</item>
<item name="android:dropDownVerticalOffset">4dp</item>
<item name="android:popupBackground">@drawable/custom_popup_background</item>
<!--<item name="android:itemBackground">@drawable/black_popup_background</item>-->
<item name="popupMenuBackground">@drawable/custom_popup_background</item>
<!--<item name="background">@color/colorPrimary</item>-->
<!--<item name="android:background">@drawable/custom_popup_background</item>-->
<item name="android:background">@color/colorPrimary</item>-->
<!-- <item name="itemBackground">@drawable/black_popup_background</item>-->
<item name="textAppearanceLargePopupMenu">@style/TextAppearance.App.Subtitle1</item>
<item name="textAppearanceSmallPopupMenu">@style/TextAppearance.App.Subtitle1</item>
</style>
<style name="TextAppearance.App.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
<item name="fontFamily">@font/amaranth</item>
<item name="android:fontFamily">@font/amaranth</item>
<item name="android:textColor">#1E2D58</item>
</style>
这是我的定制背景画
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorPrimary"/>
<corners
android:radius="16dp"/>
<padding
android:bottom="8dp"
android:top="8dp"/>
</shape>
为了测试itembackground,我创建了一个黑色的\u弹出窗口\u background.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorSecondary"/>
<corners
android:radius="16dp"/>
<padding
android:bottom="8dp"
android:top="8dp"/>
</shape>
我得到的结果是:
我不知道我做错了什么。提前谢谢大家的帮助!
暂无答案!
目前还没有任何答案,快来回答吧!