Android 字体库详解

android 自带的有字体库,在xml中可以设置,当然代码中自不必说。

1:xml使用

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="5dp"
        android:text="Typeface.createFromAsset"
        android:textColor="#333"
        android:textSize="18sp"
        android:typeface="sans" />

使用的是typeface这个属性,android中自带提供给我们的只有四种:
normalsansmonospaceserif
下面分别是对应的效果
在这里插入图片描述

2:代码实现

Typeface.create(String familyName, int style);

创建一个给定名字和指定样式的字体实例

Typeface.create(Typeface family, int style);

创建与指定的现有字体最匹配的字体对象

Typeface.createFromAsset(AssetManager mgr, String path)

从指定的字体数据创建一个新的字体。
在这里插入图片描述

Typeface.createFromFile(@Nullable File file) 
Typeface.createFromFile(String path)

根据文件或者路径创建新的字体。

3:自定义样式的textview

另外附上简单的自定义textview用来实现assets下字体的:


public class CommonTextView extends AppCompatTextView {
    private int typefaceStr;
    private Context context;

    public CommonTextView(Context context) {
        this(context, null);
    }

    public CommonTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(context, attrs, defStyleAttr);
    }

    public void init(Context context, AttributeSet attrs, int defStyleAttr) {

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonTextView, defStyleAttr, 0);
        if (typedArray != null)
            typefaceStr = typedArray.getInt(R.styleable.CommonTextView_typeface, MEDIUM);
        typedArray.recycle();
        setTypefaceStr();
    }

    public void setTypefaceStr() {
        Typeface typeFace;
        if (typefaceStr == BOLD) {
            typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/Bold.ttf");
            setTypeface(typeFace);
        } else if (typefaceStr == MEDIUM) {
            typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/Medium.ttf");
            setTypeface(typeFace);
        } else if (typefaceStr == HEAVY) {
            typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/Heavy.ttf");
            setTypeface(typeFace);
        } else if (typefaceStr == LIGHT) {
            typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/Light.ttf");
            setTypeface(typeFace);
        } else if (typefaceStr == BOOK) {
            typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/Book.ttf");
            setTypeface(typeFace);
        } else {
        }
    }

    class TypeFaceEnum {
        static final int BOLD = 0;
        static final int MEDIUM = 1;
        static final int HEAVY = 2;
        static final int BOOK = 3;
        static final int LIGHT = 4;
        static final int NORMAL = 5;
    }

}

attrs样式如下:

 <declare-styleable name="CommonTextView">
        <attr name="typeface" format="enum">
            <enum name="bold" value="0" />
            <enum name="medium" value="1" />
            <enum name="heavy" value="2" />
            <enum name="book" value="3" />
            <enum name="light" value="4" />
            <enum name="normal" value="5" />
        </attr>
    </declare-styleable>

最后是使用方式:

 <xxx.CommonTextView
        android:id="@+id/tv_type"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="5dp"
        android:text="Typeface.createFromAsset"
        android:textColor="#333"
        android:textSize="18sp"
        app:typeface="light" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值