Java Android:为我的应用程序内部的图标添加徽章

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2610384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:51:10  来源:igfitidea点击:

Android: add badge to icons internal to my app

javaandroideclipse

提问by Michael_19

I am trying to add badges to the icons in my android app. e.g. in the facebook app(for iPhone), in the home page the number of pending requests is shown on the requests icon.

我正在尝试为我的 android 应用程序中的图标添加徽章。例如,在 facebook 应用程序(适用于 iPhone)中,在主页中,待处理请求的数量显示在请求图标上。

Can someone provide any links/ideas on how to do this?

有人可以提供有关如何执行此操作的任何链接/想法吗?

Thanks

谢谢

回答by synic

Android doesn't support badges on icons.

Android 不支持图标上的徽章。

Instead, it provides the notification system (icons in the notification bar, etc), as well as the ability to create widgetsthat can be added to the users "desktop".

相反,它提供了通知系统(通知栏中的图标等),以及创建可以添加到用户“桌面”的小部件的能力。

回答by alexanderblom

If you really want to do it. Use a FrameLayoutwhich contains an ImageViewwith your icon and a TextViewwith a ninepatch drawable as background in the right corner. Add margins to the ImageViewif you want the badge to be a bit outside the icon.

如果你真的想做。使用FrameLayout包含ImageView带有您的图标的 和TextView带有可绘制的 Ninepatch 在右上角作为背景的 。ImageView如果您希望徽章稍微超出图标,则为增加边距。

回答by ranjit patel

Thanks Alexanderblom ,for the hints.i used that logic and manage to create badge on a internal imageicon.here is the xml file.and you have to just create a red circle on the drawable.

感谢 Alexanderblom 的提示。我使用该逻辑并设法在内部图像图标上创建徽章。这是 xml 文件。您只需在可绘制对象上创建一个红色圆圈。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout1"
    android:layout_width="wrap_content"
   android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:text="5"
        android:textColor="@color/black"
        android:textStyle="bold"
        android:padding="2sp"
        android:gravity="center"
        android:background="@drawable/circle"
        android:layout_gravity="top|right" />

</FrameLayout>

and the circle.xml is

和 circle.xml 是

<item>
    <shape android:shape="oval">
        <solid android:color="@android:color/black" />
    </shape>
</item>
<item android:top="1dp" android:left="1dp" android:right="1dp" android:bottom="1dp">
    <shape android:shape="oval">
        <solid android:color="@color/Red" />

    </shape>
</item>

回答by Nandakishore Shetty

check this library which adds badge to the icon/button/tab

检查这个库,它为图标/按钮/选项卡添加了徽章

https://github.com/jgilfelt/android-viewbadger

https://github.com/jgilfelt/android-viewbadger

回答by Webdma

Try this one, is good for Sony, Samsung, LG, HTC, Xiaomi, ASUS, ADW, APEX and NOVA Launchers.

试试这个,适用于 Sony、Samsung、LG、HTC、Xiaomi、ASUS、ADW、APEX 和 NOVA Launcher。

https://github.com/leolin310148/ShortcutBadger

https://github.com/leolin310148/ShortcutBadger

回答by Chirag Prajapati

Follow below define steps for add notification count to app icon

按照以下定义将通知计数添加到应用程序图标的步骤

Steps:

脚步:

  1. Add mavenCentral to your build script.

    repositories { mavenCentral() }

  2. Add dependencies in your app gradle.

    dependencies { compile 'me.leolin:ShortcutBadger:1.1.4@aar' or compile 'me.leolin:ShortcutBadger:1.1.3@aar' }

  3. Add the codes below for display notification count on app icon:

    int badgeCount = 1; ShortcutBadger.applyCount(context, badgeCount); //for 1.1.4 Or ShortcutBadger.with(getApplicationContext()).count(badgeCount); //for 1.1.3

  4. If you want to remove the badge

    ShortcutBadger.removeCount(context); //for 1.1.4 ShortcutBadger.with(getApplicationContext()).remove(); //for 1.1.3 Or ShortcutBadger.applyCount(context, 0); //for 1.1.4 ShortcutBadger.with(getApplicationContext()).count(0); //for 1.1.3

  1. 将 mavenCentral 添加到您的构建脚本中。

    存储库 { mavenCentral() }

  2. 在您的应用程序 gradle 中添加依赖项。

    依赖项 { 编译 'me.leolin:ShortcutBadger:1.1.4@aar' 或编译 'me.leolin:ShortcutBadger:1.1.3@aar' }

  3. 添加以下代码以在应用程序图标上显示通知计数:

    int 徽章计数 = 1; ShortcutBadger.applyCount(上下文,badgeCount);//对于 1.1.4 或 ShortcutBadger.with(getApplicationContext()).count(badgeCount); //对于1.1.3

  4. 如果您想移除徽章

    ShortcutBadger.removeCount(context); //对于1.1.4 ShortcutBadger.with(getApplicationContext()).remove(); //对于 1.1.3 或 ShortcutBadger.applyCount(context, 0); //对于1.1.4 ShortcutBadger.with(getApplicationContext()).count(0); //对于1.1.3