Android应用程序的默认字体

3
这是对该问题的跟进:

如何为整个Android应用程序设置默认字体族

。我有一些自定义字体,我将它们添加到资产/字体文件夹中。现在我想使用该库中的字体作为整个应用程序的默认字体。上面链接问题的被接受答案建议使用一个样式,这正是我想要的,但需要使用属性"android:fontFamily",该属性仅适用于API 16及以上版本。是否有方法也可以在旧版本API中使用样式?

您的应用程序只需要一种字体吗?还是需要粗体、小号或其他字体? - Vishal Patel
您可以使用自定义视图并在其中定义字体。这样,通过更改一个类中的字体,将会在整个类中更改它。 - Vivek Mishra
我也研究了这个问题,但是由于一个非常老的项目,我正在使用Eclipse,所以我需要Eclipse的那个库,但它只能在Gradle中使用。 - CodeMonkey
1
你可以直接从GitHub获取项目。 - Vishal Patel
有没有一种方法可以在旧的API中使用样式?-- 不行。 - CommonsWare
显示剩余4条评论
5个回答

1
如果您想为Android应用程序设置默认字体,可以使用以下方法:

Calligraphy Library

您需要在应用程序类中初始化它,这样自定义字体就可以适用于应用程序的所有活动。
只需按照库维基设置即可。

先阅读再反应,那个人说他需要 "android:fontFamily"。 - Vishal Patel
1
我实际上也研究过这个问题,但是由于一个非常老的项目,我正在使用Eclipse,所以我还需要Eclipse版本的该库。 - CodeMonkey

1

我尝试搜索解决方案并发现,对于低于16的较旧版本,您无法将自定义字体添加为默认字体。不过,内置字体可以正常使用。这里有一个可能的解决方案


1

对于 "android:fontFamily"
- 只接受由您的操作系统版本提供的默认字体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文件

编辑

不要忘记在每个根布局中添加此行。LINK

tools:ignore="MissingPrefix"

快乐编程 :)


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目录保存字体文件,位于src\main目录下的assets目录内(src\main\assets\fonts


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" />

我更喜欢不涉及类扩展的解决方案。尽管这是可能的,但如果有更好的方式,我更倾向于使用它。 - CodeMonkey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接