将自定义字体应用于整个Android应用程序?

时间:2015-06-10 17:02:54

标签: android android-fonts android-typeface

在你使用负面评论/投票之前,我会指出我已经阅读了关于这个主题的'Stackoverflow上的每个问题,并且已经应用​​了几乎所有的解决方案,但我仍然没有成功完全应用它。

所以,我问的是这个问题,因为我发现几乎所有答案都是在4 - 5年前,而且我想知道现在是否有更好的解决方案。

我重复我的问题:是否有比列出的更好的方法将整个应用程序中的整个字体系列覆盖为仅1种字体? (应用程序中使用的所有视图)?

提前致谢!

干杯!

3 个答案:

答案 0 :(得分:1)

最好的方法是将.ttf.otf字体文件放在assets文件夹中。然后派生一个自定义TextView类并一劳永逸地修复它的'字体,这样就不必在任何地方调用setTypeface()

就是这样。

答案 1 :(得分:0)

自定义TextView Java类

将此课程放在 com.util

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextViewRegular extends TextView {

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

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

    }

    public CustomTextViewRegular(Context context) {
        super(context);
        init(null);
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
//          Set type face
            Typeface myTypeface = Typeface.createFromAsset(getContext()
                    .getAssets(), "Helvetica_Neue.ttf");
            setTypeface(myTypeface);
        }
    }

}

必须 资产文件夹中的 Helvetica_Neue.ttf ,您也可以使用其他字体。

如何在 xml

使用
<com.util.CustomTextViewRegular
    android:id="@+id/txtCustom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:textColor="#727272"
    android:textSize="20dip" />

如果您想更改 字体,只需更改字体,只需输入 Java 类。

完成

答案 2 :(得分:0)

我没有找到在应用程序上设置一个字体的解决办法,所以我在每个片段/活动中为每个视图调用了setTypeface()。谢谢,我很抱歉我没有回答的很长一段时间!我当之无愧的呐喊,呵呵。

为大家欢呼!我会更加注意向将来回答问题的用户提问和我的行为。