【问题标题】:Adding custom fonts for android api 14为 android api 14 添加自定义字体
【发布时间】:2018-12-05 13:36:50
【问题描述】:

错误
错误:未找到样式属性“app:attr/fontFamily”。 消息{kind=ERROR, text=error: 样式属性 'app:attr/fontFamily' 未找到。

<style name="RadioButtonCustomStyle" parent="Widget.AppCompat.CompoundButton.RadioButton">
    <item name="android:textColorPrimaryDisableOnly">#f44336</item>
    <item name="android:textColor">#607ba3</item>
    <item name="app:fontFamily">@font/raleway_medium</item>
</style>

我已在 app/assets/font/raleway_medium.ttf

中添加了 raleway_medium.ttf

【问题讨论】:

  • 您需要像this answer一样以编程方式设置字体
  • 谢谢@RutvikBhatt,但在这个过程中,我不得不多次重复这个过程
  • 在 Utils 类中创建一个类似 public static void setTypeface(RadioButton radioButton){//your code to set typeface} 的方法,并从像 Utils.setTypeface(objRadio); 这样的任何地方调用它

标签: android font-family


【解决方案1】:

您可以通过将字体放在资产文件夹中并使用以下代码来实现:

Typeface tf = Typeface.createFromAsset(getAssets(), "your_font.ttf"); yourTextView.setTypeface(tf);

【讨论】:

  • 但我很难为多个视图重复 .setTypeFace
  • 不幸的是,这是我知道的唯一方法:(
【解决方案2】:

有一个叫做书法的图书馆。它用于像您这样的情况 - 替换旧手机上所有视图中的字体。如果我是正确的,它不支持字体资源,而只是简单的 .ttf 文件。见:https://github.com/chrisjenx/Calligraphy

我正在开发一个支持旧手机字体资源的库。它比书法更干净,但图书馆本身非常大,所以它可能不适合你。字体支持提交被很好地提取,你可以在这里找到它:https://github.com/ZieIony/Carbon/commit/baefcfb1941ecc1b4e293f31f5220ab7abaf4584

答案的基本部分是以下方法。我猜它取自 Material Components 来源。您可以将其添加到您的文本字段和按钮中,以使用它来处理 xml 属性。

private void handleFontAttribute(TypedArray appearance, int textStyle, int attributeId) {
    WeakReference<android.widget.TextView> textViewWeak = new WeakReference<>(this);
    AtomicBoolean asyncFontPending = new AtomicBoolean();
    ResourcesCompat.FontCallback replyCallback = new ResourcesCompat.FontCallback() {
        @Override
        public void onFontRetrieved(@NonNull Typeface typeface) {
            if (asyncFontPending.get()) {
                android.widget.TextView textView = textViewWeak.get();
                if (textView != null)
                    textView.setTypeface(typeface, textStyle);
            }
        }
         @Override
        public void onFontRetrievalFailed(int reason) {
        }
    };
    try {
        int resourceId = appearance.getResourceId(attributeId, 0);
        TypedValue mTypedValue = new TypedValue();
        Typeface typeface = ResourcesCompat.getFont(getContext(), resourceId, mTypedValue, textStyle, replyCallback);
        if (typeface != null) {
            asyncFontPending.set(true);
            setTypeface(typeface, textStyle);
        }
    } catch (UnsupportedOperationException | Resources.NotFoundException ignored) {
    }
}

【讨论】:

    【解决方案3】:

    在您的文件夹字体 (app/res/font) 中添加您的字体。

    之后,您可以使用 textview 并设置字体

    <TextView
        android:id="@+id/ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/YourFont"
        android:text="@string/YourText"
        android:textColor="@color/YourColor"
        android:textSize="20dp" />
    

    在您的风格中,您可以尝试将“app:fontFamily”更改为“android:fontFamily”

    希望这会有所帮助

    【讨论】:

    • 我的解决方案很有效,为什么你把我放在 -1 没有解释 xD ?
    • 我没有检查,但 Android 开发者页面显示您的解决方案不适用于 API 14 - 也许这就是原因
    猜你喜欢
    • 2013-07-14
    • 2016-08-11
    • 1970-01-01
    • 2012-03-24
    • 2010-09-26
    • 2021-05-23
    • 2021-06-22
    • 2012-02-05
    • 1970-01-01
    相关资源
    最近更新 更多