您可以在Android中设置fontFamily变量吗?

时间:2019-06-25 21:41:42

标签: android fonts font-family

我想知道是否可以像在Strings中那样在Android Studio中设置fontFamily变量。

我不想做任何自定义字体之类的事情。我只想拥有能够一次更改每个textView的fontFamily的便利。

到目前为止,我找不到字体标签。我希望你们中的一些可以帮助我。

最诚挚的问候,并提前致谢:)

2 个答案:

答案 0 :(得分:0)

将字体otf文件放入/ assets / fonts,创建用于处理字体的utils:

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");
        }
    }
}

打开时初始化字体创建您的MainAcitivity

TypefaceUtils.initTypeface(this);

在需要的位置设置字体,例如:

textView.setTypeface(TypefaceUtils.getMyFont1());

答案 1 :(得分:0)

制作一个自定义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();
    }
}
}
相关问题