更改默认弹出菜单子菜单箭头

5

我正在使用默认的PopupMenu。我已经在我的XML样式中对其进行了自定义,现在它具有深色风格。但是我现在有一个问题:请看我准备好的这个截图:

enter image description here

正如您所看到的,箭头有些难以看清,我真的希望现在能避免使用弹出窗口。有没有办法将它改成白色箭头?

2个回答

2

很久没有解决这个问题了,但是如果其他人遇到同样的问题,你可以利用样式来解决。

看起来箭头的颜色由android:textColorSecondary控制,因此如果您正在以编程方式生成弹出菜单,您可以像这样做(使用Kotlin):

val contextThemeWrapper = ContextThemeWrapper(context, R.style.PopupMenuStyle)
val popupMenu = PopupMenu(contextThemeWrapper, view)
val menu = popupMenu.menu
menu.addSubMenu(groupId, itemId, order, groupTitle)
menu.add(groupId, itemId, order, title1)
menu.add(groupId, itemId, order, title2)
etc...

在styles.xml中定义你的PopupMenuStyle,如下所示:

<style name="PopupMenuStyle" parent="@style/<some.parent.style>">
    <!--this makes an the arrow for a menu's submenu white-->
    <item name="android:textColorSecondary">@android:color/white</item>
</style>

1

自定义主题并设置listMenuViewStyle

<style name="AppTheme" parent="Theme.MaterialComponents.Light">
    <item name="listMenuViewStyle">@style/listMenuViewStyle</item>
</style>

设置子菜单箭头

<style name="listMenuViewStyle" parent="Base.Widget.AppCompat.ListMenuView">
    <item name="subMenuArrow">@drawable/icon_sub_menu</item>
</style>

将自定义主题应用于活动

<activity android:name=".material.MyTopAppBarActivity"
    android:theme="@style/AppTheme"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接