4

我开发了一个非常庞大的应用程序,现在我需要为应用程序中的所有控件使用自定义字体。所以我想知道一次性更改字体的更好方法。该应用程序有一百多个 XML 布局。而且我无法将所有控件更改为具有自定义字体的自定义组件。请提供在不更改 XML 中所有控件的情况下更改字体的解决方案。

4

1 回答 1

11

做这样的事情

pacage com.prac;
class MyFontedTextView extends TextView {
    public FontedTextView(Context context) {
        super(context);
        init();
    }

    public FontedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

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

    private void init() {
     String otfName = "MyCustomOtfFileWhichIPutInAssetsFolder.otf";
     Typeface font = Typeface.createFromAsset(context.getAssets(), otfName);
     this.setTypeface(font);
    }
}

现在从您的 TextViews 将其全部替换为 xml 文件

<com.prac.MyFontedTextView ....        instead of <TextView

您必须重新进行此更改才能应用

也适用于按钮文本的情况。按钮也是 TextView 的子类所以同样适用于按钮

希望这有助于或可以引导您找到您正在寻找的解决方案

于 2012-04-30T18:29:10.777 回答