2016-02-06 2 views
1

Привет, я вижу, что некоторые приложения создают и показывают пользовательское уведомление с пользовательским шрифтом, и я искал для него это, и я нашел this question.
, но это не работает для меня это мой кодСоздать пользовательское уведомление с пользовательским типом лица в android

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); 
} 

и мой 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); 
} 
} 

, но когда я использую этот код ничего не происходит, и уведомление показывает с DEFUALT шрифтом.
так может кто-нибудь мне помочь?

+0

Вы установили шрифт шрифта, который вы хотите показать. Вы должны включить файл .ttf в свои анализы и передать это как строку в свой класс customTypefaceSpan – Aziz

+0

yes на этой строке «sb.setSpan (новый CustomTypefaceSpan (« », Constants.molla), 0, sb.length() - 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE); "Constants.molla моего типа лицо – max

+0

Я думаю, что нужно что-то передать в строке семье, а – Aziz

ответ

1

Я думаю, вам нужно сделать альтернативный способ отображения вашего пользовательского текста, я нашел это сообщение https://stackoverflow.com/a/4411060/1007087 Проверьте это, и я надеюсь, что вы найдете это полезным. Хотя он создает растровое изображение, вы можете выполнить свою настройку.

Смежные вопросы