在 Android 上将 progressDialog 字体更改为自定义

Change progressDialog font to custom on Android

我想做的是将进度条消息的字体从默认更改为自定义。 我已经尝试了很多技术但都失败了。

我尝试通过样式更改它,然后将此样式应用于我的对话框:

<style name="StyledDialog" parent="@android:style/Theme.Panel">
    <item name="android:background">@android:color/transparent</item>
    <item name="android:alertDialogStyle">@style/CustomStyle</item>
    <item name="fontPath">fonts/Lato-Regular.ttf</item> // not working
</style>

<style name="CustomStyle">
    <item name="fontPath">fonts/Lato-Regular.ttf</item> // not working
</style>

然后我尝试在 TextView 中获取消息,但我得到的是 null

dialog = new ProgressDialog(getContext(), R.style.StyledDialog); //tried with styles here.
dialog.create(); //yep, create the dialog, see that dialogs's onCreate method was executed;
TextView view = (TextView) dialog.findViewById(R.id.message); // getting null
Typeface face = Typeface.createFromAsset(getAssets(),
        "fonts/epimodem.ttf");
view.setTypeface(face); //null pointerException

我正在使用 Calligraphy 库更改我拥有的所有 textView 的字体。 那么你知道如何改变我对 progressDialog 的喜爱吗

您可以使用 SpannableString 而不使用 stiles.xml

ProgressDialog dialog = new ProgressDialog(this);
dialog.create();

SpannableString ss=  new SpannableString("Your message text here");
Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/epimodem.ttf");
ss.setSpan(new RelativeSizeSpan(1.0f), 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new CustomTypefaceSpan("", typeFace), 0, ss.length(), 0);

dialog.setMessage(ss);
dialog.show();

CustomTypefaceSpan.java

@SuppressLint("ParcelCreator")
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 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);
    }
}

工作得很好:

 dialog = new ProgressDialog(getContext());
        String message = getContext().getString(R.string.building_route);
        SpannableString spannableString =  new SpannableString(message);

        CalligraphyTypefaceSpan typefaceSpan = new CalligraphyTypefaceSpan(TypefaceUtils.load(getContext().getAssets(), "fonts/Lato-Regular.ttf"));
        spannableString.setSpan(typefaceSpan, 0, message.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        dialog.setMessage(spannableString);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();