我在我的应用程序中使用Roboto light字体。要设置字体,我必须添加android:fontFamily="sans-serif-light"到每个视图。有什么方法来声明Roboto字体作为默认字体家族整个应用程序?我试过这样做,但似乎不管用。

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

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:fontFamily">sans-serif-light</item>
</style>

当前回答

随着Android Oreo的发布,您可以使用支持库来实现这一目标。

签入你的应用构建。Gradle,如果你有支持库>= 26.0.0 添加"font"文件夹到你的资源文件夹,并添加你的字体 在你的应用程序主样式中引用你的默认字体家族: <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <项目名称= " android: fontFamily " > @font / your_font > < /项目 <item name="fontFamily">@font/your_font</item> <!——目标android sdk版本< 26和> 14如果主题不是AppCompat——> > < /风格

更多详细信息请访问https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html。

其他回答

要改变你的应用字体遵循以下步骤:

在res目录中创建一个新目录并命名为font。 插入字体.ttf/。在字体文件夹otf,确保字体名称是小写字母和下划线。 Inside res -> values -> styles.xml Inside <resources> -> <style>添加你的字体<item name="android:fontFamily">@font/font_name</item>。

现在你所有的应用文本都应该是你添加的字体。

这在Android Studio中非常非常简单。

在这个方法中,你需要验证你的minsdkverison。minsdkversion >=16

在“res”文件夹中创建“font”文件夹。在android studio中新建>文件夹>字体文件夹。

将您的字体文件上传到该字体文件夹。

在你的style.xml文件中,在“Base application theme”的style下添加这一行。 <项目名称= " android: fontFamily " > @font / ubuntubold > < /项目

更多的细节: https://coderog.com/community/threads/how-to-set-default-font-family-for-entire-android-app.72/

这是我的项目的工作,来源https://gist.github.com/artem-zinnatullin/7749076

在资产文件夹内创建字体目录,然后复制您的自定义字体到字体目录,例如我使用trebuchet.ttf;

创建一个类TypefaceUtil.java;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;

import java.lang.reflect.Field;
public class TypefaceUtil {

    public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) {
        try {
            final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);

            final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
            defaultFontTypefaceField.setAccessible(true);
            defaultFontTypefaceField.set(null, customFontTypeface);
        } catch (Exception e) {

        }
    }
}

在styles.xml中编辑主题

<item name="android:typeface">serif</item>

My styles.xml中的示例

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:typeface">serif</item><!-- Add here -->
    </style>


    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>
</resources>

最后,在Activity或Fragment中onCreate调用TypefaceUtil.java

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TypefaceUtil.overrideFont(getContext(), "SERIF", "fonts/trebuchet.ttf");
    }

不谈性能,对于自定义字体,你可以有一个递归方法循环通过所有的视图和设置字体,如果它是一个TextView:

public class Font {
    public static void setAllTextView(ViewGroup parent) {
        for (int i = parent.getChildCount() - 1; i >= 0; i--) {
            final View child = parent.getChildAt(i);
            if (child instanceof ViewGroup) {
                setAllTextView((ViewGroup) child);
            } else if (child instanceof TextView) {
                ((TextView) child).setTypeface(getFont());
            }
        }
    }

    public static Typeface getFont() {
        return Typeface.createFromAsset(YourApplicationContext.getInstance().getAssets(), "fonts/whateverfont.ttf");
    }
}

在你的所有活动中,在setContentView之后将当前ViewGroup传递给它,就完成了:

ViewGroup group = (ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content);
Font.setAllTextView(group);

对于片段,也可以做类似的事情。

将这行代码添加到res/value/styles.xml中

<item name="android:fontFamily">@font/circular_medium</item>

整个样式看起来就像这样

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:fontFamily">@font/circular_medium</item>
</style>

将“circular_medium”更改为您自己的字体名称。