【问题标题】:Change the font of all the textviews in my application?更改我的应用程序中所有文本视图的字体?
【发布时间】:2013-10-30 18:54:05
【问题描述】:

我想知道是否有一种方法可以一次性更改 android 应用程序中所有文本视图的字体?我所说的所有文本视图是指以编程方式或动态创建的文本视图以及使用 XML 布局文件(拖放)单独创建的文本视图?

我知道我可以创建一个具有不同所需字体的新主题并使用它。但我只能看到主题适用于程序中动态创建的文本视图,而不适用于 XML 布局中的。

能否请您告诉我是否有任何解决方案,或者唯一的选择是手动更改每个文本视图的字体。

【问题讨论】:

  • 为此,您必须制作自己的自定义 TextView 并应用字体现在在整个应用程序中使用此 TextView...

标签: android android-layout textview android-theme android-styles


【解决方案1】:

最简单的方法是扩展 TextView 小部件:

public class FontTextView extends TextView {

private String mTypefacePath;

public FontTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setAttrs(context, attrs, defStyle);
    init(context);
}

public FontTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setAttrs(context, attrs, 0);
    init(context);
}

public FontTextView(Context context) {
    super(context);
    init(context);
}

private void setAttrs(Context context, AttributeSet attrs, int defStyle) {
    if (isInEditMode())
        return;
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FontTextView, defStyle, 0);
    mTypefacePath = a.getString(R.styleable.FontTextView_typeface);
    a.recycle();
}

private void init(Context context) {
    if (!TextUtils.isEmpty(mTypefacePath)) {
        try {
            setTypeface(Typeface.createFromAsset(context.getAssets(),
                    mTypefacePath));
        } catch (Exception ex) {
            // could not create the typeface from path
        }
    } 
}}

您还需要定义您的typeface 属性。 看看this,看看有用的解释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多