浅析Android加载字体包及封装的方法
2020-05-01 12:06
1406 查看
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="测试" />
效果
到此这篇关于浅析Android加载字体包及封装的方法的文章就介绍到这了,更多相关android加载字体包封装内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
相关文章推荐
- Android异步加载图像小结 (含线程池,缓存方法)
- Android 加载图片内存溢出解决方法
- 将HTML5封装成android应用APK文件若干方法
- Javascript var o={x:function(){},y:function(){}}封装方式的加载后即执行的方法。
- android-servlet-mysql servlet代码实现用户登陆 (结合上一篇jdbc封装里面的方法)
- Android webView 中loadData方法加载 带中文时出现乱码
- Android WebView封装及定制js的alert,confirm和prompt对话框的方法
- 将HTML5封装成android应用APK 文件若干方法
- android的图片加载几种方法
- android异步加载图片并缓存到本地实现方法
- Android引入第三方jar包的方法及如何将自己的代码封装成一个jar库
- Android加载大分辨率图片到手机内存中的实例方法
- 将HTML5封装成android应用APK文件若干方法
- Android WebView无法加载本地html文件的解决方法
- 【PullToRefresh 系列一基本使用方法】 Android上拉加载下拉刷新控件详解
- 将HTML5封装成android应用APK 文件若干方法
- Android异步加载图像(含线程池,缓存方法)
- Android加载对话框,异步执行代码的封装类
- 【转】将HTML5封装成android应用APK 文件若干方法
- Android大图加载方法