首页 > 其他 > 详细

自定义TextView带有各类.ttf字体的TextView

时间:2017-05-12 11:16:48      阅读:391      评论:0      收藏:0      [点我收藏+]

最近项目遇到了将普通文字转化为带有字体样式的文字,这里就涉及到了.ttf文件,我上网百度了不少资料最终终于实现了,现在想想其实并不复杂

1,你需要下载一种.ttf字体文件,你可以从网上找到一种字体的.ttf

文件,放在assets中,比如取名为ll.ttf

2.下面我们可以自定义TextView了,比较简单,设置一下我们导入的.ttf文件即可

 1 public class FontsTextView extends android.support.v7.widget.AppCompatTextView {
 2     Context context;
 3 
 4     public FontsTextView(Context context) {
 5         super(context);
 6         this.context = context;
 7         init();
 8     }
 9 
10     public FontsTextView(Context context, AttributeSet attrs) {
11         super(context, attrs);
12         this.context = context;
13         init();
14     }
15 
16     public FontsTextView(Context context, AttributeSet attrs, int defStyleAttr) {
17         super(context, attrs, defStyleAttr);
18         this.context = context;
19         init();
20     }
21 
22     private void init() {
23         setTypeface(Typeface.createFromAsset(getContext().getAssets(), "wangangziti.ttf"));
24     }
25 
26 }

3.下面直接使用就可以啦!

1 <com.egojit.android.spsp.views.FontsTextView
2                 android:id="@+id/samenameresult_name"
3                 android:layout_width="wrap_content"
4                 android:layout_height="wrap_content"
5                 android:textSize="40sp"
6                 android:textColor="#000"
7                 android:text=""
8                 />

就当做正常的TextView使用即可,显示出来的文字就是你下载的文字字体

自定义TextView带有各类.ttf字体的TextView

原文:http://www.cnblogs.com/wangying222/p/6844467.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!