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


当前回答

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

我的三星s5在使用默认字体时也遇到了同样的问题。(与其他字体的工作很好)

我设法使它工作通过设置字体(“sans”为例)在XML文件,为每个Textview或按钮

<TextView
android:layout_width="match_parent"
android:layout_height="39dp"
android:textColor="@color/abs__background_holo_light"
android:textSize="12sp"
android:gravity="bottom|center"
android:typeface="sans" />

在MyApplication类中:

public class MyApplication extends Application {
    @Override
    public void onCreate() {
    TypefaceUtil.overrideFont(getApplicationContext(), "SANS_SERIF",
    "fonts/my_font.ttf");
    }
}

希望能有所帮助。

其他回答

我想改进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();
        }
    }
}

是的,还有反思。这是可行的(基于这个答案):

(注意:这是一个变通办法,因为缺乏对自定义字体的支持,所以如果你想改变这种情况,请在这里对android问题进行星号投票)。注意:不要在这个问题上留下“我也是”的评论,当你这样做的时候,每个看过它的人都会收到一封电子邮件。所以请给它打上星号。

import java.lang.reflect.Field;
import android.content.Context;
import android.graphics.Typeface;

public final class FontsOverride {

    public static void setDefaultFont(Context context,
            String staticTypefaceFieldName, String fontAssetName) {
        final Typeface regular = Typeface.createFromAsset(context.getAssets(),
                fontAssetName);
        replaceFont(staticTypefaceFieldName, regular);
    }

    protected static void replaceFont(String staticTypefaceFieldName,
            final Typeface newTypeface) {
        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();
        }
    }
}

然后你需要重载一些默认字体,例如在一个应用程序类:

public final class Application extends android.app.Application {
    @Override
    public void onCreate() {
        super.onCreate();
        FontsOverride.setDefaultFont(this, "DEFAULT", "MyFontAsset.ttf");
        FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf");
        FontsOverride.setDefaultFont(this, "SERIF", "MyFontAsset3.ttf");
        FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset4.ttf");
    }
}

当然,如果你使用相同的字体文件,你可以改进一下,只加载一次。

然而,我倾向于只覆盖一个,说“MONOSPACE”,然后设置一个样式来强制字体应用程序宽:

<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:typeface">monospace</item>
    </style>
</resources>

发射21 Android 5.0

我已经调查了评论中的报告,它不起作用,它似乎与主题机器人:theme . material . light不兼容。

如果这个主题对你来说不重要,那就用一个旧的主题,例如:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:typeface">monospace</item>
</style>

为萨玛林工作。Android:

类:

public class FontsOverride
{
    public static void SetDefaultFont(Context context, string staticTypefaceFieldName, string fontAssetName)
    {
        Typeface regular = Typeface.CreateFromAsset(context.Assets, fontAssetName);
        ReplaceFont(staticTypefaceFieldName, regular);
    }

    protected static void ReplaceFont(string staticTypefaceFieldName, Typeface newTypeface)
    {
        try
        {
            Field staticField = ((Java.Lang.Object)(newTypeface)).Class.GetDeclaredField(staticTypefaceFieldName);
            staticField.Accessible = true;
            staticField.Set(null, newTypeface);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

应用程序实现:

namespace SomeAndroidApplication
{
    [Application]
    public class App : Application
    {
        public App()
        {

        }

        public App(IntPtr handle, JniHandleOwnership transfer)
            : base(handle, transfer)
        {

        }

        public override void OnCreate()
        {
            base.OnCreate();

            FontsOverride.SetDefaultFont(this, "MONOSPACE", "fonts/Roboto-Light.ttf");
        }
    }
}

风格:

<style name="Theme.Storehouse" parent="Theme.Sherlock">
    <item name="android:typeface">monospace</item>
</style>

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

我的三星s5在使用默认字体时也遇到了同样的问题。(与其他字体的工作很好)

我设法使它工作通过设置字体(“sans”为例)在XML文件,为每个Textview或按钮

<TextView
android:layout_width="match_parent"
android:layout_height="39dp"
android:textColor="@color/abs__background_holo_light"
android:textSize="12sp"
android:gravity="bottom|center"
android:typeface="sans" />

在MyApplication类中:

public class MyApplication extends Application {
    @Override
    public void onCreate() {
    TypefaceUtil.overrideFont(getApplicationContext(), "SANS_SERIF",
    "fonts/my_font.ttf");
    }
}

希望能有所帮助。

最后,谷歌意识到这个问题的严重性(将自定义字体应用到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!