如何检查Android设备是否具有自定义字体集

时间:2014-10-08 05:09:47

标签: android fonts

如果设备有自定义字体集,我需要设置自定义字体(如在samsung s3和其他设备中),否则我需要设置Roboto。我知道如何设置这两种字体,但我需要检查条件是否存在自定义字体集。问题不在于使用createFromAssets方法进行设置。

这是我用过的代码

private static final Typeface lightTypeface = Typeface.createFromAsset(ChatApplication
        .getContext().getAssets(), "fonts/Roboto-Light.ttf");

private static final Typeface mediumTypeface = Typeface.createFromAsset(ChatApplication
        .getContext().getAssets(), "fonts/Roboto-Medium.ttf");

private static final Typeface boldTypeface = Typeface.createFromAsset(ChatApplication
        .getContext().getAssets(), "fonts/Roboto-Bold.ttf");

private static final Typeface regularTypeface = Typeface.createFromAsset(ChatApplication
        .getContext().getAssets(), "fonts/Roboto-Regular.ttf");

public static Typeface getLightTypeface() {
    return lightTypeface;
}

public static Typeface getMediumTypeface() {
    return mediumTypeface;
}

public static Typeface getBoldTypeface() {
    return boldTypeface;
}

public static Typeface getRegularTypeface() {
    return regularTypeface;
}

1 个答案:

答案 0 :(得分:1)

使用这种方式将字体设置为textviews

Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf"));
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setTypeface(tf);