在溢出菜单图标上添加徽章 - Android

发布于 2025-01-09 02:20:56 字数 243 浏览 1 评论 0原文

有没有简单的方法可以在工具栏上的溢出菜单(三点菜单)图标上添加简单的徽章/点?

输入图片这里的描述

我试图向用户展示里面有一个新的菜单项。请注意,我应该能够以编程方式添加/删除它。任何帮助表示赞赏。

Is there any easy way of adding a simple badge/dot on the Overflow Menu (three-dot menu) icon on the Toolbar?

enter image description here

I'm trying to show the user that there is a new menu item inside. Note that I should be able to add/remove this programmatically. Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖上得阳光 2025-01-16 02:20:56

首先,在styles.xml中自定义溢出按钮样式:(

<!-- Application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
    <!-- Your theme customization... -->
    <item name="android:actionOverflowButtonStyle">@style/MyActionOverflowButtonStyle</item>
</style>

<!-- Here you choose the ID of the overflow icon. -->
<item type="id" name="myActionOverflowButtonID"/>

<!-- Style to replace action bar overflow icon. -->
<style name="MyActionOverflowButtonStyle" parent="Widget.AppCompat.ActionButton.Overflow">
    <item name="android:id">@id/myActionOverflowButtonID</item>
</style>

灵感来自如何将 android 上的三点按钮更改为其他按钮,但使用 android:id 而不是 android:src 在样式中)

然后,添加一个 徽章:

// Get the button view
val view = findViewById<View>(R.id.myActionOverflowButtonID)
// Create and customize a badge, e.g:
val badgeDrawable = BadgeDrawable.create(context).apply {
    backgroundColor = getColor(R.color.primary)
    horizontalOffset = 30
    verticalOffset = 30
    number = 2
}
// Attach the badge to the button
// For this API, you need to update Material components dependency to 1.3.0 or later
BadgeUtils.attachBadgeDrawable(badgeDrawable, view)

First, customize the overflow button style in styles.xml:

<!-- Application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
    <!-- Your theme customization... -->
    <item name="android:actionOverflowButtonStyle">@style/MyActionOverflowButtonStyle</item>
</style>

<!-- Here you choose the ID of the overflow icon. -->
<item type="id" name="myActionOverflowButtonID"/>

<!-- Style to replace action bar overflow icon. -->
<style name="MyActionOverflowButtonStyle" parent="Widget.AppCompat.ActionButton.Overflow">
    <item name="android:id">@id/myActionOverflowButtonID</item>
</style>

(inspired by How to change three dots button on android to other button, but with android:id instead of android:src in the style)

Then, add a badge:

// Get the button view
val view = findViewById<View>(R.id.myActionOverflowButtonID)
// Create and customize a badge, e.g:
val badgeDrawable = BadgeDrawable.create(context).apply {
    backgroundColor = getColor(R.color.primary)
    horizontalOffset = 30
    verticalOffset = 30
    number = 2
}
// Attach the badge to the button
// For this API, you need to update Material components dependency to 1.3.0 or later
BadgeUtils.attachBadgeDrawable(badgeDrawable, view)
孤独难免 2025-01-16 02:20:56

您可以使用简单的 BadgeDrawable

val badgeDrawable = BadgeDrawable.create(context).apply {
  // configure (set background color, offsets, etc) 
}

并稍后添加/删除它:

BadgeUtils.attachBadgeDrawable(badgeDrawable, toolbar, menuItemId)

BadgeUtils.detachBadgeDrawable(badgeDrawable, toolbar, menuItemId)

You can use simple BadgeDrawable:

val badgeDrawable = BadgeDrawable.create(context).apply {
  // configure (set background color, offsets, etc) 
}

and add/remove it later:

BadgeUtils.attachBadgeDrawable(badgeDrawable, toolbar, menuItemId)

or

BadgeUtils.detachBadgeDrawable(badgeDrawable, toolbar, menuItemId)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文