简体   繁体   English

在Android中使用自定义类型Face创建自定义通知

[英]Create custom notification with custom type face in android

Hi I see some App create and show a custom notification with custom typeface and I googling for that and I found this question . 嗨,我看到一些App创建并显示带有自定义字体的自定义通知,为此我进行了搜索,发现了这个问题
but that does not work for me this is my code 但这对我不起作用,这是我的代码

public static void CustomNotificationV(Context context,
                                       String message,boolean enter) {

    SpannableStringBuilder sb = new SpannableStringBuilder(message);

    sb.setSpan(new CustomTypefaceSpan("", Constants.molla), 0, sb.length() - 1,
            Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

    int icon=R.drawable.ex;

    // create new id
    Date date = new Date();
    int notificationid = (int) date.getTime();
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    Intent notificationIntent = new Intent(context, Splash.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context,
            notificationid, notificationIntent, 0);
    RemoteViews contentView = new RemoteViews(context
            .getApplicationContext().getPackageName(),
            R.layout.custom_notification);
    contentView.setImageViewResource(R.id.leftimage,
            icon);

    contentView.setTextViewText(R.id.message_custom_notification, sb);
    notification.contentView = contentView;
    notification.contentIntent = intent;

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(notificationid, notification);
}

and my CustomTypefaceSpan class 和我的CustomTypefaceSpan类

package tools;

import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;

/**
 * Created by mk-rad on 08/02/2015.
 */

public class CustomTypefaceSpan extends TypefaceSpan {
private final Typeface newType;

public CustomTypefaceSpan(String family, Typeface type) {
    super(family);
    newType = type;
}

@Override
public void updateDrawState(TextPaint ds) {
    applyCustomTypeFace(ds, newType);
}

@Override
public void updateMeasureState(TextPaint paint) {
    applyCustomTypeFace(paint, newType);
}

private static void applyCustomTypeFace(Paint paint, Typeface tf) {
    int oldStyle;
    Typeface old = paint.getTypeface();
    if (old == null) {
        oldStyle = 0;
    } else {
        oldStyle = old.getStyle();
    }

    int fake = oldStyle & ~tf.getStyle();
    if ((fake & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }

    if ((fake & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(tf);
}
}

but when I use this code nothing happens and notification shows with the defualt typeface. 但是当我使用此代码时,什么也没发生,并且通知显示为带有默认字体。
so can anyone help me about this? 有人可以帮我吗?

I think you should do an alternate way of displaying your custom text, I have found this post https://stackoverflow.com/a/4411060/1007087 Check this and I hope you might find this useful. 我认为您应该采取另一种方式来显示您的自定义文本,我发现这篇文章https://stackoverflow.com/a/4411060/1007087对此进行检查,希望对您有用。 Though it is creating a bitmap you can do your customization. 尽管它正在创建位图,但是您可以进行自定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM