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

1:xml使用

    

使用的是typeface这个属性,android中自带提供给我们的只有四种:
normalsansmonospaceserif
下面分别是对应的效果
Android 字体库详解_第1张图片

2:代码实现

Typeface.create(String familyName, int style);

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

Typeface.create(Typeface family, int style);

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

Typeface.createFromAsset(AssetManager mgr, String path)

从指定的字体数据创建一个新的字体。
Android 字体库详解_第2张图片

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样式如下:

                                                                                             

最后是使用方式:

 

更多相关文章

  1. Android中通过typeface设置字体
  2. android基础--TextView详解
  3. android DatePicker样式设置
  4. Android引用ttf图标字体库
  5. iphone开发之常用控件的使用详解
  6. Android中字体颜色的设置

随机推荐

  1. shell脚本实战之编译mysql
  2. 【OGG】关于在一套复制环境中使用不同版
  3. 2021最新linux云计算课程大纲
  4. Mysql Cluster7.4.6安装与配置
  5. 【MySQL】MySQL的四种事务隔离级别
  6. 01.C语言学习(控制结构)
  7. Oracle 10gR2分析函数
  8. Linux常用的shell命令汇总
  9. 【Linux】shell脚本实战-if多分支条件语
  10. 【shell】shell脚本实战-awk基本介绍