我需要使用特定的字体为我的整个应用程序。我有。ttf文件相同。 是否有可能将此设置为默认字体,在应用程序启动,然后在应用程序的其他地方使用它?当设置,我如何使用它在我的布局xml ?


当前回答

我想改进weston对API 21 Android 5.0的回答。

导致

在API 21下,大多数文本样式包括fontFamily设置,如:

<style name="TextAppearance.Material">
     <item name="fontFamily">@string/font_family_body_1_material</item>
</style>

它应用默认的Roboto常规字体:

<string name="font_family_body_1_material">sans-serif</string>

原来的答案没有应用monospace字体,因为android:fontFamily的优先级高于android:typeface属性(引用)。 使用Theme.Holo。*是一个有效的解决方案,因为里面没有android:fontFamily设置。

解决方案

自从Android 5.0把系统字体放在静态变量typeface中。sSystemFontMap(引用),我们可以使用相同的反射技术来替换它:

protected static void replaceFont(String staticTypefaceFieldName,
        final Typeface newTypeface) {
    if (isVersionGreaterOrEqualToLollipop()) {
        Map<String, Typeface> newMap = new HashMap<String, Typeface>();
        newMap.put("sans-serif", newTypeface);
        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 staticField = Typeface.class
                    .getDeclaredField(staticTypefaceFieldName);
            staticField.setAccessible(true);
            staticField.set(null, newTypeface);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

其他回答

我建议扩展TextView,并始终使用您的自定义TextView在您的XML布局或任何地方,你需要一个TextView。在你的自定义TextView,覆盖setTypeface

@Override
public void setTypeface(Typeface tf, int style) {
    //to handle bold, you could also handle italic or other styles here as well
    if (style == 1){
        tf = Typeface.createFromAsset(getContext().getApplicationContext().getAssets(), "MuseoSans700.otf");
    }else{
        tf = Typeface.createFromAsset(getContext().getApplicationContext().getAssets(), "MuseoSans500.otf");
    }
    super.setTypeface(tf, 0);
}

对于Android O,现在可以直接从XML中定义,我的bug现在已经关闭!

详情请看这里

TL; diana:

首先,必须将字体添加到项目中

其次,添加一个字体系列,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
</font-family>

最后,您可以在布局或样式中使用字体:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/lobster"/>

<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
    <item name="android:fontFamily">@font/lobster</item>
</style>

享受吧!

为了改变textview的默认字体家族,重写textViewStyle在你的应用主题。

要在fontFamily中使用自定义字体,请使用支持库中的字体资源。

该功能是在Android 26中添加的,但通过supportlib向后移植到旧版本。

https://developer.android.com/guide/topics/resources/font-resource.html https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html#using-support-lib

最后,谷歌意识到这个问题的严重性(将自定义字体应用到UI组件),他们为它设计了一个干净的解决方案。

首先,你需要更新到支持库26+(你可能还需要更新你的gradle{4.0+}, android studio),然后你可以创建一个新的资源文件夹称为字体。在这个文件夹中,您可以放置您的字体资源(.tff,…) 然后你需要覆盖默认的应用程序他们,并强制您的自定义字体:)

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:fontFamily">@font/my_custom_font</item>
</style>

注意:如果你想要支持API超过16的设备,你必须使用app命名空间而不是android!

android中有一个很棒的自定义字体库:Calligraphy

这里有一个如何使用它的示例。

在Gradle中,你需要将这一行放入应用程序的构建中。gradle文件:

dependencies {
    compile 'uk.co.chrisjenx:calligraphy:2.2.0'
}

然后创建一个扩展Application的类,并编写如下代码:

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                        .setDefaultFontPath("your font path")
                        .setFontAttrId(R.attr.fontPath)
                        .build()
        );
    }
} 

在activity类中将这个方法放在onCreate之前:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

最后一件事你的清单文件应该是这样的:

<application
   .
   .
   .
   android:name=".App">

它将改变整个活动,以您的字体!既简单又干净!