【问题标题】:Setting custom font to Textview将自定义字体设置为 Textview
【发布时间】:2016-12-28 14:55:23
【问题描述】:

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

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 中的文本变为bold。我不知道为什么会这样。而且这是非常不一致的,不会每次都发生。

【问题讨论】:

    标签: android custom-font android-typeface


    【解决方案1】:

    尝试将其添加到您的 init 中:

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

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

    【讨论】:

    • 如何缓存字体?
    • @ShreenivasChikati 加载一次并保留对它的引用。
    • 好的,但是 int flags = getPaintFlags() | Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG; setPaintFlags(标志);没有帮助我
    • @ShreenivasChikati 您可以编辑您的问题以包含问题的屏幕截图吗?
    猜你喜欢
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 2015-10-17
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多