Andorid 设置 自定义 字体

1. 现在吧字体读出来
     放在 Application中  要整成单例模式
      private static Typeface EnglishAndNumTypeface ;
      private static Typeface ChineseTypeface ;

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

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


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

public class TextViewWithChineseFont extends TextView {

      public TextViewWithChineseFont(Context context, AttributeSet attrs, int defStyleAttr) {
             super(context, attrs, defStyleAttr);
            init();
      }

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

      public TextViewWithChineseFont(Context context) {
             super(context);
            init();
      }

      private void init() {
             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" />
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章