我在我的应用程序中使用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 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/

其他回答

不谈性能,对于自定义字体,你可以有一个递归方法循环通过所有的视图和设置字体,如果它是一个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);

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

阅读下面的更新

我有嵌入一个新的字体同样的问题,最后得到它与扩展TextView和设置字体内部的工作。

public class YourTextView extends TextView {

    public YourTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public YourTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public YourTextView(Context context) {
        super(context);
        init();
    }

    private void init() {
        Typeface tf = Typeface.createFromAsset(context.getAssets(),
            "fonts/helveticaneue.ttf");
        setTypeface(tf);
    }
}

你必须改变TextView元素以后从到在每个元素。如果你在Eclipse中使用UI-Creator,有时它不会正确显示TextViews。是唯一对我有用的东西…

更新

现在,我使用反射改变字体在整个应用程序没有扩展TextViews。看看这篇SO帖子

更新2

从API级别26开始,可以在“支持库”中使用

android:fontFamily="@font/embeddedfont"

进一步信息:XML字体

这在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://github.com/sunnag7/FontStyler

<com.sunnag.fontstyler.FontStylerView
              android:textStyle="bold"
              android:text="@string/about_us"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingTop="8dp"
              app:fontName="Lato-Bold"
              android:textSize="18sp"
              android:id="@+id/textView64" />

将这行代码添加到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”更改为您自己的字体名称。