【问题标题】:Change progressDialog font to custom on Android在Android上将progressDialog字体更改为自定义
【发布时间】:2017-03-06 05:42:27
【问题描述】:

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

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

<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的喜爱

【问题讨论】:

  • 您是否尝试创建自定义对话框?
  • @Alexey move TextView view = (TextView) dialog.findViewById(R.id.message);显示对话框后。对话框.show();在此之后找到 textview 并且不会为空
  • 你在使用 AlertDialog 吗??
  • 请看一下这个答案:stackoverflow.com/a/16045641/1533933 并尝试首先使用 spans 更改进度对话框消息的颜色/大小(以确保字体或类似的东西没有问题)。成功后 - 从这里尝试 TypeFace span:stackoverflow.com/a/4826885/1533933 以使用自定义字体。希望对你有帮助
  • 成功了,谢谢。考虑发布答案。

标签: android android-styles android-progressbar


【解决方案1】:

你可以使用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);
    }
}

【讨论】:

    【解决方案2】:

    像魅力一样工作:

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

    【讨论】:

    • 谢谢你,最好链接源。需要添加依赖依赖 { compile 'uk.co.chrisjenx:calligraphy:2.2.0' } 在Android中自定义字体的简单方法...github.com/chrisjenx/Calligraphy
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 2011-10-04
    • 2023-04-02
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    相关资源
    最近更新 更多