Android 使用自定義字體


整個項目要使用第三方字體首先將字體文件放到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));
        
    }
}
View Code
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;
    }
}
View Code

使用方法

<CustomTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"  
                android:text="我是自定義字體"
                android:textColor="@color/login_font_hit"
                android:textSize="14.0sp" />
View Code

這樣就實現了項目字體統一風格


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM