浅析Android加载字体包及封装的方法
作者:不近视的猫 时间:2022-01-08 22:32:26
TextView加载字体包
在 Android 中,若需要使得某个TextView
加载字体包,使用以下方式即可:
Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/Bold.otf");
textView.setTypeface(typeFace);
至于字体包的位置:
通过以上方法,可以使得一个TextView
加载某种字体包,但是,还有这种需求:
部分
TextView
加载字体包每个
TextView
加载的字体包不一定一样
这时,我们就需要稍微封装下,将其封装成一个自定义TextView
类,若需要使用字体包,则加载该类,同时,可以根据xml
里面的值,从而加载不同的字体包。
封装
定义属性值
首先,我们需要从xml
里面获取值,因此,需要在attr
中进行属性值的定义:
<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="测试" />
效果
来源:https://blog.csdn.net/m0_46278918/article/details/105822861
标签:android,加载,字体包,封装
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IDEA教程之Activiti插件图文详解
2023-11-14 23:06:45
![](https://img.aspxhome.com/file/2023/6/59726_0s.png)
详解用Eclipse如何创建Web项目
2023-11-11 05:41:01
![](https://img.aspxhome.com/file/2023/3/59393_0s.png)
Java for循环的妙用之鸡兔同笼问题
2023-02-03 14:07:11
![](https://img.aspxhome.com/file/2023/7/96267_0s.png)
java自带的MessageDigest实现文本的md5加密算法
2023-10-08 03:35:29
设置JavaScript自动提示-Eclipse/MyEclipse
2022-06-15 12:41:05
![](https://img.aspxhome.com/file/2023/8/84628_0s.jpg)
Jmeter非GUI模式运行分布式测试
2021-11-08 12:42:07
SpringBoot中并发定时任务的实现、动态定时任务的实现(看这一篇就够了)推荐
2023-01-21 13:58:45
详解Spring中的FactoryBean
2022-06-08 05:29:55
关于Flyweight模式应用实践的相关介绍
2021-07-28 21:50:34
深入浅出讲解Java集合之Map接口
2023-10-14 20:52:46
![](https://img.aspxhome.com/file/2023/6/62816_0s.png)
Android 加载大图及多图避免程序出现OOM(OutOfMemory)异常
2022-05-06 18:32:06
springboot搭建访客管理系统的实现示例
2023-09-02 13:10:41
![](https://img.aspxhome.com/file/2023/3/58463_0s.png)
Java线程的调度与优先级详解
2023-04-30 13:48:13
详解SpringBoot的事务管理
2022-01-15 13:39:26
![](https://img.aspxhome.com/file/2023/0/74750_0s.png)
Android使用OkHttp发送post请求
2022-12-04 13:24:57
Java实现图像分割功能
2022-04-10 22:27:15
![](https://img.aspxhome.com/file/2023/6/104206_0s.jpg)
Android使用WebView播放flash的方法
2021-08-10 04:46:51
![](https://img.aspxhome.com/file/2023/7/113437_0s.png)
Java日常练习题,每天进步一点点(2)
2023-08-17 22:46:19
![](https://img.aspxhome.com/file/2023/5/63025_0s.png)
C# 最基础知识介绍--多态
2022-07-10 14:34:36
![](https://img.aspxhome.com/file/2023/1/79111_0s.jpg)
浅析Android 快速实现图片压缩与上传功能
2022-10-15 23:58:01