较低api上的androidx自定义字体

时间:2018-10-15 11:28:44

标签: android xml fonts

我在Kotlin应用中使用AndroidX,并且一直在尝试添加自定义字体。我在res / font中具有.ttf文件和2个字体家族(v26和普通)的文件夹。 v26文件包含android:前缀,而另一个文件具有app:前缀。

我还在gradle中添加了appcompat和legacy-support的实现,但是不知何故我仍然无法使字体在Android 6.0上正确显示(在较新的设备上运行)。我在AppTheme中将字体系列设置为:

User.aggregate([
    {
      $lookup: {
        from: "balance",
        localField: "fname",
        foreignField: "fname",
        as: "result"
      }
    },
    {
      $unwind: "$result"
    },
    {
      $lookup: {
        from: "dept",
        localField: "fname",
        foreignField: "fname",
        as: "deptresult"
      }
    },
    {
      $unwind: "$deptresult"
    },
    {
      $project: {
        fname: 1,
        salary: 1,
        "result.balance": 1,
        "deptresult.dept": 1
      }
    }
  ]).exec((err, users) => {
    if (err) throw err;
    res.send(users);
  });

我不知道我还能尝试什么。有人遇到过同样的问题吗?

1 个答案:

答案 0 :(得分:1)

我认为您不能在旧版API中将fontFamily用于自定义字体。

但是,您可以创建自己的TextView(扩展默认视图)并设置自定义字体:

public class MyTextView extends TextView {

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

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

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

private void init() {
    if (!isInEditMode()) {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/custom.ttf");
        setTypeface(tf);
    }
}

}

或者设置这样的字体:

Typeface typeface=Typeface.createFromAsset(getAssets(), "fonts/custom.ttf");
myTextView.setTypeface(typeface);