在整个应用程序中一次应用字体系列文件效果

时间:2017-12-02 12:00:31

标签: android android-typeface

我的资产文件夹中有字体文件roboto。我想立刻将正常字体更改为roboto。如果我使用如下字体:

 TextView forgotPwd = (TextView) findViewById(R.id.tv_forgot_pwd_text);

     Typeface type = Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf");
            forgotPwd.setTypeface(type);

然后我必须手动检查并为每个文本视图提供相同的代码。但我需要的是,如果我设置了Typeface或它应该在整个应用程序的文本或文本视图中反映的任何内容。

现在我用了

 public class MyApp extends Application {
              @Override
              public void onCreate() {
                 super.onCreate();
                TypefaceUtil.overrideFont(getApplicationContext(), "Roboto", "fonts/Roboto-Regular.ttf");
              }
              }

I used application class and also took the help of some custom class but its not working .

公共类TypefaceUtil {

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("Roboto", 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);
        }
    }
}

}

现在style.xml是

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorControlNormal">#FFFFFF</item>
        <item name="colorControlActivated">#FFFFFF</item>
        <item name="android:textColorHint">@color/black</item>
        <item name="android:typeface">Roboto</item>
    </style>

在清单中:

    android:name="pkgname.MyApp"

但是我仍然没有得到我的申请中的反映,请让我知道我失踪的地方。并指导我

3 个答案:

答案 0 :(得分:1)

  

如果你想这样做,那么你应该只扩展View类(TextView)并添加你想要的特定字体系列,你需要创建CustomTextView类并在这里更改你也可以在attr文件夹中添加一些其他属性。

答案 1 :(得分:1)

你可以Calligraphy

这很容易使用。

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                            .setDefaultFontPath("Roboto-Regular.ttf")
                            .setFontAttrId(R.attr.fontPath)
                            .build()

创建BaseActivity或将其添加到您的所有活动,即通话

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

答案 2 :(得分:0)

只需尝试将字体名称更改为默认值。

前: -
     TypefaceUtil.overrideFont(getApplicationContext(),&#34; Roboto&#34;,&#34; fonts / Roboto-Regular.ttf&#34;);

将其更改为     TypefaceUtil.overrideFont(getApplicationContext(),&#34;默认&#34;,&#34; fonts / Roboto-Regular.ttf&#34;);

最好按照此链接 Is it possible to set a custom font for entire of application?