我正在開發Android
應用程序,但我無法爲應用程序Notification
標題設置自定義字體。如何在通知中應用字體
我不想使用任何自定義佈局Notification
。我想在默認Notification
標題中應用字體。
我不知道應該可能與否。 在此先感謝。
我正在開發Android
應用程序,但我無法爲應用程序Notification
標題設置自定義字體。如何在通知中應用字體
我不想使用任何自定義佈局Notification
。我想在默認Notification
標題中應用字體。
我不知道應該可能與否。 在此先感謝。
長衝浪後,我已經弄清楚我的問題如下:
我已創建自定義佈局view_notification.xml
如下:
view_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@color/white"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/ivNotificationImage"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/iconlarge"
android:layout_gravity="center_vertical"
android:layout_margin="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/tvNotificationTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/app_name"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="16sp"
android:maxLines="1"/>
<TextView
android:id="@+id/tvMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@color/black"
android:maxLines="1"/>
</LinearLayout>
我已經創建CustomTypefaceSpan.java
爲TypeFace
如下:
CustomTypefaceSpan.java
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);
}
}
我已經轉換我的簡單文本代碼以SpannableStringBuilder
如下:
SpannableStringBuilder sb = new SpannableStringBuilder(object.getString("description") + "");
Typeface font = Typeface.createFromAsset(getAssets(), "shruti.ttf");
sb.setSpan(new CustomTypefaceSpan("", font), 0, sb.length() - 1,
Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
sendNotification(title, sb);
我已經使用RemoteViews
自定義佈局裏面有Notification
查看。
public void sendNotification(String title, SpannableStringBuilder sb) {
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.view_notification);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.iconlarge)
.setAutoCancel(true)
.setContent(remoteViews);
remoteViews.setTextViewText(R.id.tvNotificationTitle, getResources().getString(R.string.app_name));
remoteViews.setTextViewText(R.id.tvMessage, sb);
...
...
}
感謝的人,它爲我工作。讓我投票回答這個問題。 –