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

最近项目遇到了将普通文字转化为带有字体样式的文字,这里就涉及到了.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使用即可,显示出来的文字就是你下载的文字字体

posted on 2017-05-12 10:48  oooo呼呼  阅读(267)  评论(0编辑  收藏  举报