gpt4 book ai didi

安卓通知 : add typeface for title and content

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:24:24 27 4
gpt4 key购买 nike

我正在尝试在我的 NotificationCompat.Builder 中添加一个 Typeface ->setContentTitle()setContentText()。我通过

初始化了 Typeface
Typeface banglaFont = Typeface.createFromAsset(this.getAssets(), "kalpurush.ttf");

IntentService 中。为了创建 Notification,我使用了以下代码。

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

但我不明白如何在我的Notification 标题和内容中设置Typeface。任何建议或引用将不胜感激。提前致谢。

最佳答案

在通知上设置字体的最佳方法是将文本转换为位图并在 Canvas 中渲染。为了渲染自定义通知,我们希望使用远程 View 。

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

关于安卓通知 : add typeface for title and content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27373465/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com