问题描述
我试图在 NotificationCompat.Builder
- > 中添加
和 Typeface
setContentTitle() setContentText()
。我初始化字体
by
I'm trying to add a Typeface
in my NotificationCompat.Builder
->setContentTitle()
and setContentText()
. I initialized Typeface
by
Typeface banglaFont = Typeface.createFromAsset(this.getAssets(), "kalpurush.ttf");
位于 IntentService
中。要创建 Notification
,我使用下面的代码。
in IntentService
. To create Notification
i used following code.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setLargeIcon(bitmap)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(userName)
.setAutoCancel(true)
// .setStyle(
// new NotificationCompat.BigTextStyle().bigText(msg))
.setStyle(new NotificationCompat.InboxStyle())
.setVibrate(pattern).setLights(Color.BLUE, 500, 500)
.setSound(alarmSound).setContentText(msg);
mBuilder.setContentIntent(contentIntent);
Notification n = mBuilder.build();
int min = 1001;
int max = 2000;
Random r = new Random();
int randomNumber = r.nextInt(max - min + 1) + min;
int notID = randomNumber;
mNotificationManager.notify(notID, n);
但我无法理解如何设置字体
在我的通知
标题和内容中。任何建议或参考将不胜感激。
提前致谢。
But i cann't understand how can i set the Typeface
in my Notification
title and content. Any suggestions or reference would be highly appreciated.Thanks in advance.
推荐答案
这是非常有用的答案,通过创建自己的typefacespan
$ b
this is very useful answer, done by creating own typefacespan http://www.codeproject.com/Questions/567126/AndroidplusNotificationplusinplusotherpluslanguage
custom_notification.xml
custom_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_notification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
<ImageView
android:id="@+id/notification_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_margin="5dip"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/notification_text"
style="@style/NotificationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_toRightOf="@+id/notification_image"
android:maxLines="3"
android:layout_centerVertical="true"
android:ellipsize="end"
android:text="@string/app_name" />
</RelativeLayout>
android的通知方法代码
code for android to notification method
private static void generateNotification(Context context,
SpannableStringBuilder message) {
int icon = R.drawable.ic_launcher;
// 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);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.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.notification_image,
R.drawable.ic_launcher);
// contentView.setTextViewText(R.id.notification_title,
// "My custom notification title");
contentView.setTextViewText(R.id.notification_text, message);
notification.contentView = contentView;
notification.contentIntent = intent;
// notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notificationid, notification);
}
像这样调用这个方法:
call this method like this:
SpannableStringBuilder sb = new SpannableStringBuilder("মুখ্যমন্ত্রী হওয়ার পর থেকেই রাজ্যের হাতে আরও বেশি ক্ষমতা 4দেওয়ার দাবিতে বারেবারে সরব হয়েছেন তিনি");
Typeface font = Typeface.createFromAsset(getAssets(), "kalpurush.ttf");
sb.setSpan(new CustomTypefaceSpan("", font), 0, sb.length() - 1,
Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
generateNotification(context, sb);
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);
}
}
我希望这对我很有帮助
这篇关于Android通知:为标题和内容添加字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!