Android:想要使用styles.xml或任何其他选项将自定义字体设置为整个应用程序



我正在android应用程序中实现自定义字体。。我想为整个应用程序使用样式使用一种自定义字体。XML,也可以是其他选项。

在Android中没有简单的内置方法可以做到这一点。

你可能想看看书法,这是一个开源项目,可以很容易地更改整个应用程序的字体。

我遇到了同样的问题,我没有找到用.xml文件来解决这个问题的方法,最后,我继承TextView和EditText,并通过代码应用字体

  1. 将.otf文件放在项目中的资产/字体库中

  2. 创建从TextView继承的TextViewFont类

    公共类TextViewFont扩展了TextView{
    private int mType=0;

    public TextViewFont(Context context) 
    {
        this(context,null,0);
    }
    public TextViewFont(Context context, AttributeSet attrs)
    {
        this(context,attrs,0);
    }
    
    public TextViewFont(Context context, AttributeSet attrs, int defStyle)
    {
        super(context,attrs,defStyle);
        init(context,attrs);
    }
    public void setType(int type){
        this.mType= type;
        Typeface tf;
        switch (mType)
        {
        case 0:
            tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/xxx-light.otf");
            setTypeface(tf);
            break;
        case 1:
            tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/xxx-regular.otf");
            setTypeface(tf);
            break;
        case 2:
            tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/xxx-bold.otf");
            setTypeface(tf);
            break;
        }
    }
    private void init(Context context, AttributeSet attrs )
    {
        if (attrs != null)
        {
            TypedArray a = context.getTheme().obtainStyledAttributes(
                    attrs,
                    R.styleable.TextViewFont,
                    0, 0);
            try 
            {
                TypedValue tv = new TypedValue();
                if (a.getValue(0, tv))
                {
                    mType = (int)tv.data;
                }
                mType = a.getInteger(R.styleable.TextViewFont_fontType,0);
            } 
            finally 
            {
                a.recycle();
            }
        }
        if (!isInEditMode())
        {
            Typeface tf;
            switch (mType)
            {
            case 0:
                tf = Typeface.createFromAsset(context.getAssets(), "fonts/xxx-light.otf");
                setTypeface(tf);
                break;
            case 1:
                tf = Typeface.createFromAsset(context.getAssets(), "fonts/xxx-regular.otf");
                setTypeface(tf);
                break;
            case 2:
                tf = Typeface.createFromAsset(context.getAssets(), "fonts/xxx-bold.otf");
                setTypeface(tf);
                break;
            }
        }
    }
    

    }

3.在xml布局文件中使用示例

在使用自定义视图的文件头中添加

     xmlns:custom="http://schemas.android.com/apk/res-auto"

并将自定义类添加为任何其他视图元素

<xxxx.xxxx.xxxx.TextViewFont
        android:id="@+id/xxxxx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="abcdEFGH 123"
        android:textAppearance="@style/xxxxxxx"
        custom:fontType="Regular" />
  1. 定义自定义xml属性vlaues/attrs.xml

<resources>
    <declare-styleable name="TextViewFont">
        <attr name="fontType" format="enum">
            <enum name="Light" value="0"/>
            <enum name="Regular" value="1"/>
            <enum name="Bold" value="2"/>
        </attr>
    </declare-styleable>
</resources>

最新更新