在DialogFragment中更改ProgressDialog的字体字体

我可以知道是否可以在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; } 

通过从ProgressDialoginheritance来创build自定义类可能是其中一种方法。 不过,我想知道有没有更好的select? 可悲的是,我们没有ProgressDialog.Builder

我尝试过的其中一个select是

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

Related of "在DialogFragment中更改ProgressDialog的字体字体"

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

从文档中可以看出: http : //developer.android.com/reference/android/app/DialogFragment.html

 public static class MyDialogFragment extends DialogFragment { static MyDialogFragment newInstance() { return new MyDialogFragment(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.hello_world, container, false); View tv = v.findViewById(R.id.text); ((TextView)tv).setText("This is an instance of MyDialogFragment"); return v; } } 

我怀疑你可以为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)); } } } 

这假定你有自定义字体放在assets/fonts/verdana.ttf (如果使用_default()方法)

其中一个build议的解决scheme如下。 但我不认为这是一个好方法。 任何进一步的build议都非常受欢迎。

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

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

Utils.setCustomTypeface

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

虽然这是一个老问题,但我已经拼凑出一些可能有助于未来访问者的信息。

在开始之前,这不是一个即时的设置。 它只能在ProgressDialog初始化时完成,并且只能用于内置字体。 现在有人说,让我们开始做生意! 首先,我们需要使用android:Theme.Holo.Dialog的父项创build样式,如下所示,在您的styles.xml文件中。

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

为了使这个答案尽可能地接近这个问题的答案,我正在研究一个名为书法的图书馆,并且通过对这个问题的回应,看起来这个开发者计划在对话框中增加这个function。 书法可能最终是你想要使用,但现在你将不得不使用自定义视图黑客非系统字体。 无论如何,我希望这有助于一些正在寻找这个答案的访问者。