【问题标题】:How to set the font from Styles.xml in android如何在 android 中从 Styles.xml 设置字体
【发布时间】:2017-08-18 07:35:12
【问题描述】:

我有什么:

  1. 我有一个继承 AppCompatTextView 的自定义类。
  2. 我在attires.xml 中定义了一个自定义属性textformat 和 我正在传递我需要从 xml 设置的字体

样式文件

<style name="HeaderFilterName">
        <item name="android:src">@drawable/back_button</item>
        <item name="android:text">@string/str_filter_edit</item>
        <item name="android:gravity">center</item>
        <item name="android:textSize">@dimen/Header_Filter_Name_Text_size</item>
        <item name="android:layout_weight">1</item>
    </style>

XML

    <customViews.CustomTftTextView
    android:id="@+id/txtScreenNameId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:textformat="fonts/sf_san_fransisco.ttf"
    style="@style/HeaderFilterName"/>

attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="customfont">
        <attr name="textformat" format="string"/>
    </declare-styleable>
</resources>

CustomTftTextView.java

public class CustomTftTextView extends AppCompatTextView {
    private String text;

    public CustomTftTextView(final Context context) {
        this(context, null);
        Initialize(text,context);
    }

    public CustomTftTextView(final Context context, final AttributeSet attrs) {
        this(context, attrs, 0);
        text = context.getResources().obtainAttributes(attrs, R.styleable.customfont).getString(R.styleable.customfont_textformat);
        Initialize(text,context);
    }

    public CustomTftTextView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        text = context.getResources().obtainAttributes(attrs, R.styleable.customfont).getString(R.styleable.customfont_textformat);
        Initialize(text,context);
    }

    private void Initialize(String format, Context context) {

        Typeface mTypeface;
        if (format != null)
        {
            mTypeface = Typeface.createFromAsset(context.getAssets(), format);
        }
        else
        {
            mTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/sf_san_fransisco.ttf");
        }
        setTypeface(mTypeface, Typeface.NORMAL);
        setLineSpacing(0.0f, 1.4f);
    }
}

虽然上面的代码完美运行,但如果我将 app:textformat 移动到 style 文件中,字体未设置。

<style name="HeaderFilterName">
            <item name="android:src">@drawable/back_button</item>
            <item name="android:text">@string/str_filter_edit</item>
            <item name="textformat">@string/custom_font_medium </item>
<item name="android:gravity">center</item>
            <item name="android:textSize">@dimen/Header_Filter_Name_Text_size</item>
            <item name="android:layout_weight">1</item>
        </style>

strings.xml

<string name="custom_font_medium">fonts/sf_san_fransisco.ttf</string>

如何正确实现这一目标

【问题讨论】:

    标签: java android xml android-styles


    【解决方案1】:

    如果您查看Resources#obtainAttributes() method 的文档,它会说:

    从 AttributeSet 中检索一组基本属性值,而不是使用主题和/或样式资源对其进行样式设置。

    要获得应用样式的属性,请改用Context#obtainStyledAttributes() 方法。还建议保留对该返回的引用,这样您就可以在完成后recycle() 它。例如:

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.customfont);
    text = a.getString(R.styleable.customfont_textformat);
    a.recycle();
    

    【讨论】:

      【解决方案2】:

      确保您在 styles.xml 中使用了如下命名空间

      <resources xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app='http://schemas.android.com/apk/res-auto'>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多