【问题标题】:Calligraphy library by chrisjenx is not workingchrisjenx 的书法库无法正常工作
【发布时间】:2023-03-29 01:27:01
【问题描述】:

在设置默认字体时,我按照他的文档的指示进行了操作:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupDefaultFont();

        setContentView(R.layout.activity_main);

        setupToolbarAndNavigationDrawer();
  }

  public void setupDefaultFont() {
        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                        .setDefaultFontPath("fonts/OpenSans-Regular.ttf")
                        .setFontAttrId(R.attr.fontPath)
                        .build()
        );
  }

我也将字体放在assets/fonts,但无济于事。 Roboto 仍然显示为默认字体,而不是 Open Sans。我尝试将它手动一个一个地应用到每个TextView,但它仍然不起作用。

关于为什么这不起作用的任何想法?

更多信息:(如果有用的话) 我的 miniSdkVersion 是 15,targetSdkVersion 是 22。 这些是我的依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:design:22.2.0'
    compile 'com.android.support:recyclerview-v7:21.0.3'
    compile 'com.android.support:cardview-v7:21.0.3'
    compile 'de.hdodenhof:circleimageview:1.2.1'
    compile 'uk.co.chrisjenx:calligraphy:2.1.0'
}

这是我正在使用的自定义主题。

<resources>
    <style name="myIIT_theme" parent="Theme.AppCompat">
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorPrimary">@color/primary</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="android:windowBackground">@color/tertiary_dark</item>
        <item name="android:activatedBackgroundIndicator">@drawable/selected_drawer</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>

【问题讨论】:

    标签: android fonts custom-font calligraphy


    【解决方案1】:

    为了使配置生效,您应该在自定义 application 类的 onCreate() 方法中设置默认字体,而不是在活动中。

    另外,https://github.com/chrisjenx/Calligraphy 处的说明通过覆盖 activity 中的方法来注入上下文,如下所示:

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

    【讨论】:

    • 谢谢!在你回答之前才发现它。应该在这里自己回答。不过还是谢谢。
    • 那么我们需要在每个Activity中重写attachBaseContext()?
    • 是的。或者创建一个通用的超类,并从中继承每个 Activity。
    • 它在 android API 26 的 appcompatViews 中不再工作了!
    【解决方案2】:

    连同@Theo 的回答,确保您在清单中注册您的自定义应用程序

    <application
      android:name=".MyApplication" <----------- HERE
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
    

    【讨论】:

      【解决方案3】:

      如 github 中的自述文件中所述, 此版本的书法已达到其生命周期,不再维护。请迁移到Calligraphy 3

      【讨论】:

        猜你喜欢
        • 2011-02-13
        • 1970-01-01
        • 2016-12-01
        • 1970-01-01
        • 2011-12-26
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多