2016-12-28 178 views
0

我使用自定義字體在我的應用程序 所以我用下面的代碼設置自定義字體

public class BrandonBlackTextView extends TextView { 

public BrandonBlackTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(); 
} 

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

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

public void init() { 
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/brandon_blk.ttf"); 
    setTypeface(tf ,1); 

} 

地方我想用我用這個textview到TextView的。有用。 但問題有時我不知道爲什麼,但textview裏面的文字變成加粗。我不知道爲什麼會發生這種情況。這非常不一致,每次都不會發生。

in the pic, you see in one row text is bold.

回答

0

嘗試添加以下內容到初始化:

int flags = getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG; 
setPaintFlags(flags); 

你也應該緩存的字體,而不是某個地方重新裝入每一個TextView的。

+0

如何緩存字體? –

+0

@ShreshivasChikati加載一次,並保留一個參考。 – Karakuri

+0

ok,但int flags = getPaintFlags()| Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG; setPaintFlags(flags);沒有幫助我 –