9

ProgressDialog内で のメッセージ表示のフォント書体を変更することは可能DialogFragmentですか?

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ことも方法の 1 つかもしれません。しかし、私は知りたいのですが、より良い代替手段はありますか? 悲しいことに、私たちは持っていませんProgressDialog.Builder

私が試した代替手段の1つは

@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() を呼び出す必要があります

4

4 に答える 4

4
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);
    }
}
于 2014-08-05T15:22:27.503 に答える
3

提案された解決策の1つは次のとおりです。しかし、これは良い方法ではないと思います。さらなる提案は大歓迎です。

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);
        }
    }
}
于 2013-03-01T18:41:48.850 に答える
3

ドキュメントからわかるように: 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()場合)

于 2013-03-01T18:30:00.487 に答える