如何在android

时间:2017-08-18 07:35:12

标签: java android xml android-styles

我有什么

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

    Stylefile

    <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>
    

    如何正确实现这个

2 个答案:

答案 0 :(得分:2)

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

  

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

要获取应用了样式的属性,请改用Context#obtainStyledAttributes()方法。建议保留对该回报的引用,以便在完成后recycle()。例如:

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

答案 1 :(得分:0)

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

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