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

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

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

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

 public class FontsTextView extends android.support.v7.widget.AppCompatTextView {
Context context; public FontsTextView(Context context) {
super(context);
this.context = context;
init();
} public FontsTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
} public FontsTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
} private void init() {
setTypeface(Typeface.createFromAsset(getContext().getAssets(), "wangangziti.ttf"));
} }

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

 <com.egojit.android.spsp.views.FontsTextView
android:id="@+id/samenameresult_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#000"
android:text=""
/>

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

05-11 22:50