gpt4 book ai didi

android - 在 DialogFragment 中更改 ProgressDialog 的字体

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:30:37 24 4
gpt4 key购买 nike

请问是否可以在 DialogFragment 中更改 ProgressDialog 消息显示的字体?

public class LoadFromCloudTaskFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
this.progressDialog = new ProgressDialog(this.getActivity());
this.progressDialog.setMessage(progressMessage);
this.progressDialog.setCanceledOnTouchOutside(false);

return progressDialog;
}

通过从 ProgressDialog 继承来创建自定义类可能是其中一种方法。但是,我想知道有没有更好的选择?遗憾的是,我们没有 ProgressDialog.Builder

我尝试过的替代方案之一是

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
this.progressDialog = new ProgressDialog(this.getActivity());
this.progressDialog.setMessage(progressMessage);
this.progressDialog.setCanceledOnTouchOutside(false);

Utils.setCustomFont(this.progressDialog.findViewById(android.R.id.message), Utils.ROBOTO_LIGHT_FONT);

return progressDialog;
}

但这会给我错误

android.util.AndroidRuntimeException: requestFeature() must be called before adding content

最佳答案

Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf");
SpannableStringBuilder spannableSB = new SpannableStringBuilder(getString(R.string.text_please_wait));
spannableSB.setSpan (new CustomTypefaceSpan("fonts/Roboto-Regular.ttf", font), 0, spannableSB.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
loadDialog = ProgressDialog.show(this, null, spannableSB, true, false);

public class CustomTypefaceSpan extends TypefaceSpan {

private final Typeface newType;

public CustomTypefaceSpan(String family, Typeface type) {
super(getContext(),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);
}
}

关于android - 在 DialogFragment 中更改 ProgressDialog 的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15164280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com