温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android加载字体包及封装的方法是什么

发布时间:2020-07-08 10:12:37 来源:亿速云 阅读:206 作者:清晨 栏目:移动开发

这篇文章将为大家详细讲解有关Android加载字体包及封装的方法是什么,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

TextView加载字体包

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

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

至于字体包的位置:

Android加载字体包及封装的方法是什么

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

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

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

封装

定义属性值

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

Android加载字体包及封装的方法是什么

<declare-styleable name="FontTextView">
  <attr name="fontType" format="enum">
   <enum name="bold" value="1" />
   <enum name="heavy" value="2" />
  </attr>
 </declare-styleable>

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

创建自定义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,使用以下方式即可:

<com.jm.core.common.widget.textview.FontTextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:fontType="bold"
  android:text="测试" />

效果

Android加载字体包及封装的方法是什么

关于Android加载字体包及封装的方法是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI