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

最佳答案

做这样的事情

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

您必须重新完成此更改才能应用

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

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

10-07 19:40
查看更多