将自定义字体设置为Textview

时间:2016-12-28 14:55:24

标签: android custom-font android-typeface

我在我的应用程序中使用自定义字体 所以我使用了以下代码

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内的文字变为粗体。我不知道为什么会这样。这是非常不一致的,每次都不会发生。

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

1 个答案:

答案 0 :(得分:0)

尝试将此添加到您的init:

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

此外,您应该在某处缓存字体,而不是为每个TextView再次加载字体。