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