Android通知:为标题和内容添加字体
我正在尝试在我的Android通知:为标题和内容添加字体,android,google-cloud-messaging,android-notifications,android-typeface,Android,Google Cloud Messaging,Android Notifications,Android Typeface,我正在尝试在我的NotificationCompat.Builder->setContentTitle()和setContentText()中添加字体。我初始化了字体 Typeface banglaFont = Typeface.createFromAsset(this.getAssets(), "kalpurush.ttf"); 在IntentService中。为了创建通知,我使用了以下代码 NotificationCompat.Builder mBuilder = new Notifica
NotificationCompat.Builder
->setContentTitle()
和setContentText()
中添加字体。我初始化了字体
Typeface banglaFont = Typeface.createFromAsset(this.getAssets(), "kalpurush.ttf");
在IntentService
中。为了创建通知
,我使用了以下代码
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);
但是我不明白如何在我的通知
标题和内容中设置字体
。如有任何建议或参考,将不胜感激。
提前谢谢 这是一个非常有用的答案,通过创建自己的字体跨度来完成
编辑:
自定义通知.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>
按如下方式调用此方法:
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);
更新:
我希望这是非常有用的设置通知字体的最佳方法是将文本转换为位图并在画布中渲染。对于渲染自定义通知,我们希望使用remoteview
custome_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp"
android:orientation="horizontal"
>
<ImageView android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="1dp"
android:src="@drawable/ic_launcher"/>
<ImageView android:id="@+id/notification_img"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="1dp" />
</LinearLayout>
public void showCustomNotifications(Context context, String text){
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels;
Bitmap myBitmap = Bitmap.createBitmap(width - 72, 84, Bitmap.Config.ARGB_4444);
Canvas myCanvas = new Canvas(myBitmap);
Paint paint = new Paint();
Typeface font2 = Typeface.createFromAsset(context.getAssets(), context.getResources().getString(R.string.sinhalaFont));
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setTypeface(font2);
paint.setStyle(Paint.Style.FILL);
//paint.setColor(Color.BLACK);
paint.setTextSize(50);
paint.setTextAlign(Align.LEFT);
myCanvas.drawText(text, 80, 60, paint);
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, "Anynews", when);
//String title = 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.getPackageName(),
R.layout.custom_notification);
contentView.setImageViewBitmap(R.id.notification_img,
myBitmap);
// 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);
}
custome_notification.xml
公共void showCustomNotifications(上下文、字符串文本){
DisplayMetrics DisplayMetrics=context.getResources().getDisplayMetrics();
int width=displayMetrics.widthPixels;
Bitmap myBitmap=Bitmap.createBitmap(宽度-72,84,Bitmap.Config.ARGB_4444);
画布myCanvas=新画布(myBitmap);
油漆=新油漆();
Typeface font2=Typeface.createFromAsset(context.getAssets(),context.getResources().getString(R.string.sinhalaFont));
paint.setAntiAlias(真);
paint.setSubpixelText(真);
油漆。设置字体(font2);
绘制.设置样式(绘制.样式.填充);
//油漆。设置颜色(颜色。黑色);
油漆.尺寸(50);
paint.setTextAlign(Align.LEFT);
myCanvas.drawText(文本,80,60,绘画);
int icon=R.drawable.ic_启动器;
//创建新id
日期=新日期();
int notificationid=(int)date.getTime();
长时间=System.currentTimeMillis();
NotificationManager NotificationManager=(NotificationManager)context.getSystemService(context.NOTIFICATION\u服务);
通知通知=新通知(图标“Anynews”,何时);
//String title=getString(R.String.app\u name);
意向通知意向=新意向(上下文,MainActivity.class);
//设置意图,使其不会启动新活动
notificationIntent.setFlags(Intent.FLAG\u活动\u清除\u顶部
|意图。标记活动(单个顶部);
PendingIntent=PendingIntent.getActivity(上下文,
notificationid,notificationIntent,0);
RemoteViews contentView=新的RemoteView(context.getPackageName(),
R.布局、定制通知);
contentView.setImageViewBitmap(R.id.notification\u img,
我的位图);
//contentView.setTextViewText(R.id.notification_title,
//“我的自定义通知标题”);
//setTextViewText(R.id.notification_text,message);
notification.contentView=contentView;
notification.contentIntent=意图;
//通知。SetLateStevenInfo(上下文、标题、消息、意图);
notification.flags |=notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notificationid,notification);
}
如果我没有弄错的话,系统广播的任何通知的字体在很大程度上取决于用户在其设置中选择的首选字体,这意味着您的字体将被忽略。这是不可能的,任何黑客攻击都可能在不同的设备上随时中断/更新包含TextView的信息,并且您可以始终在TextView上设置字体。请使用RemoteView创建自定义视图,然后将此SpannableStringBuilder设置为content text。这对我来说很有效。CustomTypefaceSpan在哪里。class@Sudo使用CustomTypefaceSpan答案中的链接class@amrut-bidri我添加了你的代码,但它在安卓4.4和安卓5上工作,但在2.3上不工作。你能给我一些帮助吗?我下载了你的字体并尝试了你的后单词,但它在2.3设备上也不工作。
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);
}
}
custome_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp"
android:orientation="horizontal"
>
<ImageView android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="1dp"
android:src="@drawable/ic_launcher"/>
<ImageView android:id="@+id/notification_img"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="1dp" />
</LinearLayout>
public void showCustomNotifications(Context context, String text){
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels;
Bitmap myBitmap = Bitmap.createBitmap(width - 72, 84, Bitmap.Config.ARGB_4444);
Canvas myCanvas = new Canvas(myBitmap);
Paint paint = new Paint();
Typeface font2 = Typeface.createFromAsset(context.getAssets(), context.getResources().getString(R.string.sinhalaFont));
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setTypeface(font2);
paint.setStyle(Paint.Style.FILL);
//paint.setColor(Color.BLACK);
paint.setTextSize(50);
paint.setTextAlign(Align.LEFT);
myCanvas.drawText(text, 80, 60, paint);
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, "Anynews", when);
//String title = 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.getPackageName(),
R.layout.custom_notification);
contentView.setImageViewBitmap(R.id.notification_img,
myBitmap);
// 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);
}