整個項目要使用第三方字體首先將字體文件放到assets文件夾下
因為整個項目要用第三方字體這里我重寫了 TextView Button EditText 三個控件
以TextView 為例代碼如下 其它控件一樣換下繼承
public class CustomTextView extends TextView { public CustomTextView(Context context) { super(context); init(context); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public CustomTextView(Context context, AttributeSet attrs, int defSyle) { super(context, attrs, defSyle); init(context); } /*** * 設置字體 * * @return */ public void init(Context context) { setTypeface(FontCustom.setFont(context)); } }
public class FontCustom { static String fongUrl = "fonts/fzltxh_gbk.ttf"; static Typeface tf; /*** * 設置字體 * * @return */ public static Typeface setFont(Context context) { if(tf==null){ tf = Typeface.createFromAsset(context.getAssets(), fongUrl); } return tf; } }
使用方法
<CustomTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我是自定義字體" android:textColor="@color/login_font_hit" android:textSize="14.0sp" />
這樣就實現了項目字體統一風格
