android 通知消息Android Notification: add typeface for title and content

2015-1-5 9:13:59     人阅读    

 

我现在在  NotificationCompat.Builder ->setContentTitle() and setContentText(). 添加 一个Typeface,我通过 Typeface banglaFont = Typeface.createFromAsset(this.getAssets(), "kalpurush.ttf"); 初始化Typeface
在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,有谁可以提供一些说明文档或者地址?


处理方法

下面的文档中非常的有用,你可以参考一下,能解答你关于 Typeface 的问题
http://www.codeproject.com/Questions/567126/AndroidplusNotificationplusinplusotherpluslanguage
EDIT:

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>

notification 方法代码如下

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

通过如下方式调用代码

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


原文地址:http://www.itmmd.com/201501/438.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。

Android KSOAP2调用.net webservice   上一篇
下一篇  android学习笔记(34)使用AlertDialog创建简单对话框
精彩回复
我要追加问题,请求站长解决!
姓名:       

《程序员app》专门为程序员量身定做!