你能在Android中设置fontFamily变量吗?

你能在Android中设置fontFamily变量吗?,android,fonts,font-family,Android,Fonts,Font Family,我想知道是否可以在Android Studio中设置fontFamily变量,就像您可以使用字符串一样 我不想做任何像定制字体这样的花哨事。我只是希望能够方便地一次更改每个textView的fontFamily 到目前为止,我还没有找到字体标签。我希望你们中的一些人能帮助我 向您致以最诚挚的问候和感谢:)将字体otf文件放入/assets/fonts,创建一个用于处理以下字体的UTIL: public class TypefaceUtils { private static Typefa

我想知道是否可以在Android Studio中设置fontFamily变量,就像您可以使用字符串一样

我不想做任何像定制字体这样的花哨事。我只是希望能够方便地一次更改每个textView的fontFamily

到目前为止,我还没有找到字体标签。我希望你们中的一些人能帮助我


向您致以最诚挚的问候和感谢:)

将字体otf文件放入/assets/fonts,创建一个用于处理以下字体的UTIL:

public class TypefaceUtils {
    private static Typeface myFont1;
    private static Typeface myFont2;

    public static Typeface getMyFont1() {
        return myFont1;
    }

    public static Typeface getMyFont2() {
        return myFont2;
    }

    public static void initTypeface(Context context) {
        if (context != null) {
            myFont1 = Typeface.createFromAsset(context.getAssets(), "fonts/myfont1.otf");
            myFont2 = Typeface.createFromAsset(context.getAssets(), "fonts/myfont2.otf");
        }
    }
}
创建MainActivity时初始化字体

TypefaceUtils.initTypeface(this);
在需要的地方设置字体,如:

textView.setTypeface(TypefaceUtils.getMyFont1());

创建自定义TextView类并在TextView中使用它

public class MetaFont extends TextView {


public MetaFont(Context context, AttributeSet attributeSet, int i) {
    super(context, attributeSet, i);
    a(attributeSet);
}

public MetaFont(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);
    a(attributeSet);
}

public MetaFont(Context context) {
    super(context);
    a(null);
}

private void a(AttributeSet attributeSet) {
    if (attributeSet != null) {
        TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attributeSet, ATTRS);
        setTypeface(Typeface.createFromAsset(getContext().getAssets(), "Comfortaa.ttf"));
        obtainStyledAttributes.recycle();
    }
}
}