Android应用的默认字体

时间:2017-04-24 11:52:02

标签: android android-fonts

这是对这个问题的跟进:

How to set default font family for entire Android app

我有一些我添加到assets / fonts文件夹的自定义字体。现在我想使用该库中的字体作为整个应用程序的默认字体。

上面链接问题的已接受答案建议使用我想要的样式,但使用仅在API 16中添加的属性"android:fontFamily"。有没有办法为旧API使用样式还有?

5 个答案:

答案 0 :(得分:1)

如果你想要Android应用程序的默认字体,你可以使用:

Calligraphy Library

并且您需要在应用程序类中初始化它,自定义字体将适用于应用程序的所有活动。

只需按照lib wiki进行设置即可。

答案 1 :(得分:1)

我尝试搜索解决方案并发现您无法将自定义字体添加为低于16的较低版本的默认字体。
它适用于内置字体。 这是一个可能的solution

答案 2 :(得分:1)

对于"android:fontFamily"
- 仅OS version LINK提供的默认字体 - 在 Android“O”之后,您可以将其与资源文件夹LINK

一起使用

所以,

如果你有现有的项目,那么@Oussaki建议的Calligraphy Library建议最好,更快捷地设置字体。

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    fontPath="fonts/Roboto-Bold.ttf"/>

整合

之后,它只需要fontPath="fonts/Roboto-Bold.ttf"

也适用于Eclipse .Aar file

修改

不要忘记将此行放在每个根布局上。 LINK

tools:ignore="MissingPrefix"

快乐编码:)

答案 3 :(得分:0)

在资源资源中添加字体ttf文件

尝试以下类自定义文本视图,并在xml文件中添加自定义类

public class CustomTextView extends TextView {


    public CustomTextView(Context context) {
        super(context);
        this.setTypeface(getTypeFace(context));
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setTypeface(getTypeFace(context));
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setTypeface(getTypeFace(context));
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.setTypeface(getTypeFace(context));
    }


    public Typeface getTypeFace(Context context) {
        Typeface font = Typeface.createFromAsset(context.getAssets(), "your_filename.ttf");
        return font;
    }
}

在xml文件中,如:

<package_name.CustomTextView
        android:id="@+id/tv_downloading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/icon_close"
        android:textSize="100dp"
        android:textStyle="bold"
        android:layout_centerInParent="true"
        android:background="@color/col_white"
        android:textColor="@color/colorPrimaryDark"
        android:visibility="visible" />

答案 4 :(得分:0)

我最终做的是首先添加这个类:

public class TypefaceUtil 
{

/**
 * Using reflection to override default typeface
 * NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
 *
 * @param context                    to work with assets
 * @param defaultFontNameToOverride  for example "monospace"
 * @param customFontFileNameInAssets file name of the font from assets
 */
public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) 
{
    final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
    {
        Map<String, Typeface> newMap = new HashMap<String, Typeface>();
        newMap.put("serif", customFontTypeface);
        try 
        {
            final Field staticField = Typeface.class.getDeclaredField("sSystemFontMap");
            staticField.setAccessible(true);
            staticField.set(null, newMap);
        } 
        catch (NoSuchFieldException e) 
        {
            e.printStackTrace();
        } 
        catch (IllegalAccessException e) 
        {
            e.printStackTrace();
        }
    } 
    else 
    {
        try 
        {
            final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
            defaultFontTypefaceField.setAccessible(true);
            defaultFontTypefaceField.set(null, customFontTypeface);
        } catch (Exception e) 
        {
            Log.e(TypefaceUtil.class.getSimpleName(), "Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
        }
    }
}

}

然后我在Application类中添加了这一行:

@Override
public void onCreate()
{
    super.onCreate();
    TypefaceUtil.overrideFont(getApplicationContext(), "serif", "fonts/Ubuntu-Regular.ttf");
}

保存字体的fonts目录位于assets内的src\main目录(src\main\assets\fonts

相关问题