zoukankan      html  css  js  c++  java
  • 自定义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使用即可,显示出来的文字就是你下载的文字字体

  • 相关阅读:
    面向对象-类
    模块04
    总结
    昨天的新的解决方法
    感冒了~ vs中py和vb实现一个小算法
    vs2015社区版不支持installshield
    网站被黑了
    2018/11/18(python)
    2018/11/14
    2018/11/12(python)
  • 原文地址:https://www.cnblogs.com/wangying222/p/6844467.html
Copyright © 2011-2022 走看看