如何在通知中应用字体

3

我正在开发一个 Android 应用程序,但是我无法为应用程序的 通知 标题设置自定义字体。

我不想为 通知 使用任何自定义布局。我想在默认的 通知 标题中应用字体。

我不知道是否可能。 谢谢提前。

2个回答

2

2

在长时间的搜索后,我找到了以下解决方法:

我创建了一个名为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);

我已经在 Notification 视图中使用了 RemoteViews 来自定义布局。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接