TextView加载字体包

在 Android 中,若需要使得某个TextView加载字体包,使用以下方式即可:

 Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/Bold.otf");  textView.setTypeface(typeFace);

至于字体包的位置:

通过以上方法,可以使得一个TextView加载某种字体包,但是,还有这种需求:

  • 部分TextView加载字体包
  • 每个TextView加载的字体包不一定一样

这时,我们就需要稍微封装下,将其封装成一个自定义TextView类,若需要使用字体包,则加载该类,同时,可以根据xml里面的值,从而加载不同的字体包。

封装

定义属性值

首先,我们需要从xml里面获取值,因此,需要在attr中进行属性值的定义:

           

这里我只定义了两种属性,大家可以根据需求进行增减。

创建自定义TextView

public class FontTextView extends AppCompatTextView { public FontTextView(Context context) {  super(context); } public FontTextView(Context context, @Nullable AttributeSet attrs) {  this(context, attrs, 0); } public FontTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {  super(context, attrs, defStyleAttr); }}

获取属性值

//获取参数  TypedArray a = context.obtainStyledAttributes(attrs,    R.styleable.FontTextView, defStyleAttr, 0);  int fontType = a.getInt(R.styleable.FontTextView_fontType, 1);

进行值判断并加载不同的字体包

private final int BOLD = 1; private final int HEAVY = 2;  String fontPath = null;  switch (fontType) {   case BOLD:    fontPath = "fonts/Bold.otf";    break;   case HEAVY:    fontPath = "fonts/Heavy.otf";    break;   default:  }  //设置字体  if (!TextUtils.isEmpty(fontPath)) {   Typeface typeFace = Typeface.createFromAsset(getContext().getAssets(), fontPath);   setTypeface(typeFace);  }

全部源码

public class FontTextView extends AppCompatTextView { private final int BOLD = 1; private final int HEAVY = 2; public FontTextView(Context context) {  super(context); } public FontTextView(Context context, @Nullable AttributeSet attrs) {  this(context, attrs, 0); } public FontTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {  super(context, attrs, defStyleAttr);  //获取参数  TypedArray a = context.obtainStyledAttributes(attrs,    R.styleable.FontTextView, defStyleAttr, 0);  int fontType = a.getInt(R.styleable.FontTextView_fontType, 1);  String fontPath = null;  switch (fontType) {   case BOLD:    fontPath = "fonts/Bold.otf";    break;   case HEAVY:    fontPath = "fonts/Heavy.otf";    break;   default:  }  //设置字体  if (!TextUtils.isEmpty(fontPath)) {   Typeface typeFace = Typeface.createFromAsset(getContext().getAssets(), fontPath);   setTypeface(typeFace);  } }}

若需要使用字体包TextView,使用以下方式即可:

效果

到此这篇关于浅析Android加载字体包及封装的方法的文章就介绍到这了,更多相关android加载字体包封装内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

更多相关文章

  1. Android使用ImageLoader异步加载网络图片(二)结合listview
  2. 如何实现一个图片加载框架
  3. [转]Android有效解决加载大图片时内存溢出的问题
  4. 下拉刷新、上拉加载实战:带你理解自定义View整个过程
  5. Android如何在初始化的时候获取加载的布局的宽高
  6. android开发,加载网络图片时 报错android.os.NetworkOnMainThread
  7. Android视图加载流程(5)之View的详细绘制流程Layout
  8. android使用webView加载本地资源
  9. Android视图加载流程(6)之View的详细绘制流程Draw

随机推荐

  1. Android数据库监听器+Demo
  2. Android(安卓)App开发之ANR异常的原因分
  3. Android(安卓)Sensor详解(1)简介与架构
  4. 出版技术书籍的经历
  5. 使用ARouter进行Android模块化开发
  6. Android(安卓)studio打开机智云APP(自动
  7. Android中获取手机IMEI,IMSI, MAC(Android(
  8. Android应用发布后的统计——百度移动统
  9. Android通过Okhttp3实现socket长连接
  10. APP切图那点事儿–详细介绍android和ios