Android 在DialogFragment中更改ProgressDialog的字体

Android 在DialogFragment中更改ProgressDialog的字体,android,Android,我可以知道在DialogFragment中更改ProgressDialog消息显示的字体吗 public class LoadFromCloudTaskFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { this.progressDialog = new ProgressDialog(this.getActivi

我可以知道在
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() 在添加内容之前


从文件中可以看出:

我怀疑您可以为DialogFragment提供自定义布局XML

在我继续使用这个实用程序类设置字体之后:

import java.util.Hashtable;
import java.util.Map;

import android.content.Context;
import android.graphics.Typeface;
import android.widget.TextView;

/**
 * Taken from bug on b.android.com
 * https://code.google.com/p/android/issues/detail?id=9904
 * <p>
 * Optimizes way to work with typefaces and avoids context related memory leak
 * */
public class Typefaces {

    private static final Map<String, Typeface> cache = new Hashtable<String, Typeface>();

    public static Typeface get(Context c, String name) {
        synchronized (cache) {
            if (!cache.containsKey(name)) {
                Typeface t = Typeface.createFromAsset(c.getAssets(),
                        String.format("fonts/%s.ttf", name));
                cache.put(name, t);
            }
            return cache.get(name);
        }
    }

    public static Typeface _default(Context c) {
        return get(c, "verdana");
    }

    public static void setFonts(Context c, TextView... tvs) {
        for (TextView t : tvs) {
            if (t != null)
                t.setTypeface(_default(c));
        }
    }

}
import java.util.Hashtable;
导入java.util.Map;
导入android.content.Context;
导入android.graphics.Typeface;
导入android.widget.TextView;
/**
*摘自b.android.com上的bug
* https://code.google.com/p/android/issues/detail?id=9904
*
*优化处理字体的方式并避免与上下文相关的内存泄漏
* */
公共类字体{
私有静态最终映射缓存=新哈希表();
公共静态字体get(上下文c,字符串名称){
已同步(缓存){
如果(!cache.containsKey(名称)){
Typeface t=Typeface.createFromAsset(c.getAssets(),
格式(“字体/%s.ttf”,名称));
cache.put(name,t);
}
返回cache.get(name);
}
}
公共静态字体\u默认值(上下文c){
返回get(c,“verdana”);
}
公共静态无效设置字体(上下文c、文本视图…电视){
用于(文本视图t:tvs){
如果(t!=null)
t、 setTypeface(_default(c));
}
}
}

假设您在
资产/font/verdana.ttf中放置了自定义字体(如果使用
\u default()
方法)

建议的解决方案之一如下。但我认为这不是一个好办法。欢迎提出进一步建议。

public class ProgressDialogEx extends ProgressDialog {
    public ProgressDialogEx(Context context) {
        super(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = this.findViewById(android.R.id.message);
        if (view != null) {
            // Shouldn't be null. Just to be paranoid enough.
            Utils.setCustomTypeface(view, Utils.ROBOTO_LIGHT_FONT);
        }
    }
}

Utils.setCustomTypeface
public static final Typeface ROBOTO_LIGHT_TYPE_FACE=Typeface.createFromAsset(MyApplication.instance().getAssets(),“font/ROBOTO LIGHT.ttf”);
公共静态void setCustomTypeface(视图、字体){
如果(查看文本视图的实例){
((文本视图)视图)。设置字体(字体);
}else if(查看EditText的实例){
((编辑文本)视图)。设置字体(字体);
}else if(视图组的视图实例){
ViewGroup ViewGroup=(ViewGroup)视图;
int count=viewGroup.getChildCount();
for(int i=0;i


虽然这是一个老问题,我已经拼凑了一些信息,可能会帮助未来的游客

在我开始之前,这不是一个即时设置。它只能在ProgressDialog初始化时完成,并且只能使用内置字体。既然有人说了,那我们就开始谈正事吧!首先,我们需要在
styles.xml
文件中创建一个带有
android:Theme.Holo.Dialog
父项的样式,如下所示

<style name="CustomFontDialog" parent="android:Theme.Holo.Dialog">
    <item name="android:typeface">monospace</item>
</style>

为了使这个答案尽可能接近这个问题的答案,我正在研究一个名为的库,看起来,开发人员计划将这个功能添加到对话框中。书法可能最终会成为您想要使用的东西,但现在您必须为非系统字体使用自定义视图。无论如何,我希望这能帮助一些和我一样在寻找答案的访问者。

我不想提供自己的观点。我更喜欢使用默认的ProgressDialog,只需更改其字体即可。如果没有,我宁愿通过重写它来创建一个自定义ProgressDialog类。@YanChengCHEOK这是个问题,因为不同的ROM和Android版本可能有不同的布局和不同的TextView id。您需要使用反射API来查找已经膨胀的布局中的所有TextView实例并设置字体。在我看来,使用自定义布局是最简单的方法。什么是Utils活动?请描述Utils这对我来说非常有效。我也做了同样的改变
public class LoadFromCloudTaskFragment extends DialogFragment {

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

        return progressDialog;
    }
public static final Typeface ROBOTO_LIGHT_TYPE_FACE = Typeface.createFromAsset(MyApplication.instance().getAssets(), "fonts/Roboto-Light.ttf");

public static void setCustomTypeface(View view, Typeface typeFace) {
    if (view instanceof TextView) {
        ((TextView)view).setTypeface(typeFace);
    } else if (view instanceof EditText) {
        ((EditText)view).setTypeface(typeFace);
    } else if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup)view;
        int count = viewGroup.getChildCount();
        for (int i = 0; i < count; i++) {
            setCustomTypeface(viewGroup.getChildAt(i), typeFace);
        }
    }
}
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);
    }
}
<style name="CustomFontDialog" parent="android:Theme.Holo.Dialog">
    <item name="android:typeface">monospace</item>
</style>
ProgressDialog prog = new ProgressDialog(new ContextThemeWrapper(context, R.style.CustomFontDialog)));