Andorid 设置 自定义 字体

来源:互联网 发布:暗黑破坏神2 mac 1.13 编辑:程序博客网 时间:2024/05/22 01:37
1. 现在吧字体读出来
     放在 Application中  要整成单例模式
      privatestaticTypefaceEnglishAndNumTypeface;
      privatestaticTypefaceChineseTypeface;

      publicstaticTypeface getEnglishAndNumTypeface(Context context) {
            if(EnglishAndNumTypeface==null) {
                  EnglishAndNumTypeface= Typeface.createFromAsset(context.getAssets(),"fonts/HelveticaLight.ttf");
            }
            returnEnglishAndNumTypeface;
      }

      publicstaticTypeface getChineseTypeface(Context context) {
            if(ChineseTypeface==null) {
                  ChineseTypeface= Typeface.createFromAsset(context.getAssets(),"fonts/Chines_lantingheijian.ttf");
            }
            returnChineseTypeface;
      }
2. 获取TextView
TextView numView = (TextView) itemView.findViewById(R.id.number);
3. 设置字体
numView .setTypeface( NiceCDApplication.getTypeface( getActivity()));


第二种方法
自己写 TextView 然后 在xml 直接使用该TextView

publicclassTextViewWithChineseFontextendsTextView {

      publicTextViewWithChineseFont(Context context, AttributeSet attrs,intdefStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
      }

      publicTextViewWithChineseFont(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
      }

      publicTextViewWithChineseFont(Context context) {
            super(context);
            init();
      }

      privatevoidinit() {
            this.setTypeface(NiceCDApplication.getChineseTypeface(getContext()));
      }
}
然后在 xml 中使用该View
<com.zuimeia.suite.nicecountdown.widget.fontview.TextViewWithChineseFont
                   android:id="@+id/introduction_title"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:layout_marginLeft="2dp"
                   android:layout_marginTop="10dp"
                   android:text="@string/introduction"
                   android:textSize="@dimen/font_size_large"/>
0 0
原创粉丝点击