自定义TextView导入外部特殊字体
2016-06-02 11:05
435 查看
前段时间由于项目需求,app需要使用特殊字体,所以在网上搜刮了一大波博客把需求完成了,但是由于并没有完美的解决问题(问题见上篇博客),几经曲折跟好基友请教了一番,毛瑟顿开,才有了一下这篇博文。
特殊字体包的导入请见上篇文章,这里就不在赘述了。接下来贴代码:
先是自定义一个textview为了接下来存放外部特殊字体,文件目录如下:
CustomTextView类文件:
FontCustom类文件:
ok,自定义textview完成,接下来就是在xml布局里边调用,直接改变TextView标签就可以了:
完成了这些操作以后,就可以运行你的项目看看效果了,不用像上篇博客一样一个一个TextView控件去stetType()了,(笑哭……)!
运行后的效果图:
是不是完美解决了,系统字体字号过大就变粗的硬伤。
特殊字体包的导入请见上篇文章,这里就不在赘述了。接下来贴代码:
先是自定义一个textview为了接下来存放外部特殊字体,文件目录如下:
CustomTextView类文件:
package com.example.administrator.customview; import android.content.Context; import android.util.AttributeSet; import android.widget.TextView; /** * Created by Administrator on 2016-06-02. * 自定义 TextView */ public class CustomTextView extends TextView { public CustomTextView(Context context) { super(context); init(context); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public CustomTextView(Context context, AttributeSet attrs, int defSyle) { super(context, attrs, defSyle); init(context); } /*** * 设置字体 * * @return */ public void init(Context context) { setTypeface(FontCustom.setFont(context)); } }
FontCustom类文件:
package com.example.administrator.customview; import android.content.Context; import android.graphics.Typeface; /** * Created by Administrator on 2016-06-02. * 特殊字体定义 */ public class FontCustom { static String fongUrl = "font/FangZhen_GBK.ttf"; static Typeface tf; /*** * 设置字体 * * @return */ public static Typeface setFont(Context context) { if(tf==null){ tf = Typeface.createFromAsset(context.getAssets(), fongUrl); } return tf; } }
ok,自定义textview完成,接下来就是在xml布局里边调用,直接改变TextView标签就可以了:
<customview.CustomTextView android:id="@+id/UserCenter_txt" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginTop="2dp" android:layout_weight="6" android:text="@string/UserCenter_txt" />
完成了这些操作以后,就可以运行你的项目看看效果了,不用像上篇博客一样一个一个TextView控件去stetType()了,(笑哭……)!
运行后的效果图:
是不是完美解决了,系统字体字号过大就变粗的硬伤。
相关文章推荐
- 一套Semantic-UI的前端界面
- android高斯模糊
- 《Head First C#》学习 ---第三章总结
- window对象
- discuz搬家后报错SQL:SELECT value FROM [Table]vars WHERE name=’noteexists1′的解决办法
- LR12.53—第6课:运行负载测试
- 10 款最佳剪贴板管理器
- iOS静态库介绍
- Qt 获取Windows内存信息
- Java堆内存的10个要点
- GitHub前50名的Objective-C动画相关库
- GITHUB来获得UE4源代码
- MySQL使用可重复读作为默认隔离级别的原因之一
- JavaScript判断数字是否为质数的方法汇总
- Jquery on方法绑定事件后执行多次的解决方法
- Zookeeper应用场景
- Android studio 大BUG,Plugin is too old, please update to a more recent version
- apue编程之参考du代码利用递归写的一个简单的du命令的源代码
- iOS--动画--GitHub前50名的Objective-C动画相关库
- linux几种查看日志的方法