0
A
回答
1
這被稱爲徽章。 你可以使用這個庫來支持徽章。 Leolin Shortcut Badger 這也是Android ActionItem Badged 我認爲它不適用於所有設備。雖然三星使用它。 您可以在這裏閱讀Posts與您的相似。
1
如果你想添加徽章圖標
BadgeDrawable是一個自定義繪製
package com.wisilica.cms.utitlty.viewHelper;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import com.wisilica.cms.R;
/**
* Created by Godwin Joseph on 02-11-2016 11:49 for Cms_Android application.
*/
public class BadgeDrawable extends Drawable {
private Paint mBadgePaint;
private Paint mBadgePaint1;
private Paint mTextPaint;
private Rect mTxtRect = new Rect();
private String mCount = "";
private boolean mWillDraw;
public BadgeDrawable(Context context) {
float mTextSize = context.getResources().getDimension(R.dimen.badge_text_size);
mBadgePaint = new Paint();
mBadgePaint.setColor(Color.RED);
mBadgePaint.setAntiAlias(true);
mBadgePaint.setStyle(Paint.Style.FILL);
mBadgePaint1 = new Paint();
mBadgePaint1.setColor(ContextCompat.getColor(context.getApplicationContext(), R.color.white));
mBadgePaint1.setAntiAlias(true);
mBadgePaint1.setStyle(Paint.Style.FILL);
mTextPaint = new Paint();
mTextPaint.setColor(Color.WHITE);
mTextPaint.setTypeface(Typeface.DEFAULT);
mTextPaint.setTextSize(mTextSize);
mTextPaint.setAntiAlias(true);
mTextPaint.setTextAlign(Paint.Align.CENTER);
}
@Override
public void draw(Canvas canvas) {
if (!mWillDraw) {
return;
}
Rect bounds = getBounds();
float width = bounds.right - bounds.left;
float height = bounds.bottom - bounds.top;
// Position the badge in the top-right quadrant of the icon.
/*Using Math.max rather than Math.min */
float radius = ((Math.max(width, height)/2))/2;
float centerX = (width - radius - 1) + 5;
float centerY = radius - 5;
if (mCount.length() <= 2) {
// Draw badge circle.
canvas.drawCircle(centerX, centerY, (int) (radius + 7.5), mBadgePaint1);
canvas.drawCircle(centerX, centerY, (int) (radius + 5.5), mBadgePaint);
} else {
canvas.drawCircle(centerX, centerY, (int) (radius + 8.5), mBadgePaint1);
canvas.drawCircle(centerX, centerY, (int) (radius + 6.5), mBadgePaint);
// canvas.drawRoundRect(radius, radius, radius, radius, 10, 10, mBadgePaint);
}
// Draw badge count text inside the circle.
mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect);
float textHeight = mTxtRect.bottom - mTxtRect.top;
float textY = centerY + (textHeight/2f);
if (mCount.length() > 2)
canvas.drawText("99+", centerX, textY, mTextPaint);
else
canvas.drawText(mCount, centerX, textY, mTextPaint);
}
/*
Sets the count (i.e notifications) to display.
*/
public void setCount(String count) {
mCount = count;
// Only draw a badge if there are notifications.
mWillDraw = !count.equalsIgnoreCase("0");
invalidateSelf();
}
@Override
public void setAlpha(int alpha) {
// do nothing
}
@Override
public void setColorFilter(ColorFilter cf) {
// do nothing
}
@Override
public int getOpacity() {
return PixelFormat.UNKNOWN;
}
}
並添加代碼 { BadgeDrawable徽章;
// Reuse drawable if possible
Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge);
if (reuse != null && reuse instanceof BadgeDrawable) {
badge = (BadgeDrawable) reuse;
} else {
badge = new BadgeDrawable(mContext);
}
badge.setCount(count);
icon.mutate();
icon.setDrawableByLayerId(R.id.ic_badge, badge);
}
您可以在任何視圖
0
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@null"
android:text="Button" />
<FrameLayout
android:id="@+id/badgecountlayout"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="left"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/badgecount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/round_notification"
android:gravity="center"
android:text="99" />
</FrameLayout>
</FrameLayout>
round_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<gradient android:startColor="@color/yellow_dark" android:endColor="@color/yellow_dark"
android:angle="270"/>
</shape>
相關問題
- 1. 在viewpager上添加通知徽章數
- 2. 推送通知 - 應用徽章圖標
- 3. 在GridView中爲未讀通知添加徽章Android
- 4. 如何在UIBarbutton項上添加徽章?
- 5. 推送通知徽章
- 6. 圖標徽章通知
- 7. 通用Windows應用程序中的徽章通知
- 8. 如何在多個本地通知觸發時增加應用圖標徽章?
- 9. 如何清除通知徽章計數重新安裝應用
- 10. 如何在Android中使用推送通知實現徽章
- 11. 如何在android中使用徽章通知?
- 12. 增加應用徽章數量而不顯示推送通知
- 13. 如何在android 5.0工具欄中添加徽章/計數通知在android 5.0棒棒糖中使徽章/計數的工具欄通知圖標
- 14. 通過shields.io徽章添加SonarQube覆蓋
- 15. 增加應用徽章號
- 16. 解除通知中心的通知後更新通知徽章
- 17. 如何添加徽章列出
- 18. 未讀/讀通知徽章在網頁
- 19. UITableView Cell中的通知徽章
- 20. Silverlight/xaml中的通知圖標徽章
- 21. 如何在用戶點擊徽章時管理通知
- 22. 向Trello添加徽章
- 23. 在我的應用程序上實施徽章通知
- 24. Material-UI/React:如何正確添加徽章到AppBar的通知按鈕?
- 25. 如何在Android應用程序(如iPhone)上創建通知圖標徽章
- 26. iOS - 用戶禁用通知的應用如何獲取徽章更新?
- 27. 如何在收到推送通知時更新徽章號碼
- 28. 如何清除徽章數量,同時保持通知中心
- 29. 推送通知徽章自動增量
- 30. 辭退引導徽章通知
設置Drwabale是否有可能在應用程序?我的意思是,應用程序的按鈕,不只是應用程序的圖標。 – kinyas