为什么默认情况下会概述Android的TextView自定义粗体字体?

时间:2016-04-07 13:35:56

标签: android text

我在整个应用程序中使用自定义字体如下(我在Stack Overflow上找到了这种方法):

public class Application extends android.app.Application {
    private static final String DEFAULT_NORMAL_BOLD_FONT_FILENAME = "HelveticaNeueCyrMedium.otf";
    private static final String DEFAULT_NORMAL_BOLD_ITALIC_FONT_FILENAME = "HelveticaNeueCyrBoldItalic.otf";
    private static final String DEFAULT_NORMAL_ITALIC_FONT_FILENAME = "HelveticaNeueCyrItalic.otf";
    private static final String DEFAULT_NORMAL_NORMAL_FONT_FILENAME = "HelveticaNeueCyrRoman.otf";

    @Override
    public void onCreate() {
        super.onCreate();

        try {
            setDefaultFonts();
        } catch (NoSuchFieldException e) {
         …
        }
    }

    private void setDefaultFonts() throws NoSuchFieldException, IllegalAccessException {
        final Typeface bold = Typeface.createFromAsset(getAssets(), DEFAULT_NORMAL_BOLD_FONT_FILENAME);
        final Typeface italic = Typeface.createFromAsset(getAssets(), DEFAULT_NORMAL_ITALIC_FONT_FILENAME);
        final Typeface boldItalic = Typeface.createFromAsset(getAssets(), DEFAULT_NORMAL_BOLD_ITALIC_FONT_FILENAME);
        final Typeface normal = Typeface.createFromAsset(getAssets(), DEFAULT_NORMAL_NORMAL_FONT_FILENAME);

        Field defaultField = Typeface.class.getDeclaredField("DEFAULT");
        defaultField.setAccessible(true);
        defaultField.set(null, normal);

        Field defaultBoldField = Typeface.class.getDeclaredField("DEFAULT_BOLD");
        defaultBoldField.setAccessible(true);
        defaultBoldField.set(null, bold);

        Field sDefaults = Typeface.class.getDeclaredField("sDefaults");
        sDefaults.setAccessible(true);
        sDefaults.set(null, new Typeface[]{normal, bold, italic, boldItalic});
    }
}

在API级别为14,15的模拟器上以及某些真实设备(如Lenovo A789或HTC Desire S)上TextView绘制粗体字体(字母细节,请参阅screenshot)。

对于API级别21+和其他设备,它会在没有大纲的情况下按预期绘制粗体文本。 (粗体文本由TextView&at at xml布局设置:android:textStyle="bold"

有人可以告诉我如何禁用轮廓文本并正常绘制(如this)?

0 个答案:

没有答案