有关与Android的TextView相关的字体大小的更多信息

时间:2015-04-26 13:39:18

标签: android font-size

我想知道字体大小,特别是与Android的密度无关像素有关。字体大小为10的字体大小为11?

我问这个问题,因为我遇到了下面描述的问题。

我已在我的Android应用中定义了此自定义TextView,以支持我的语言和英语。 TextView根据应用的语言设置更改字体。

public class MyanmarTextView extends TextView {

    public MyanmarTextView(Context context) {
        super(context);
        String fontName = "fonts/OpenSans.ttf";
        String language = context
                .getSharedPreferences("org.bitbucket.infovillafoundation.denko", Context.MODE_PRIVATE)
                .getString("org.bitbucket.infovillafoundation.denko.language", "en");
        if (language.equals("my"))
            fontName = "fonts/WINNWAB.ttf";
        Typeface face = Typeface.createFromAsset(context.getAssets(), fontName);
        this.setTypeface(face);
    }

    public MyanmarTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        String fontName = "fonts/OpenSans.ttf";
        String language = context
                .getSharedPreferences("org.bitbucket.infovillafoundation.denko", Context.MODE_PRIVATE)
                .getString("org.bitbucket.infovillafoundation.denko.language", "en");
        if (language.equals("my"))
            fontName = "fonts/WINNWAB.ttf";
        Typeface face = Typeface.createFromAsset(context.getAssets(), fontName);
        this.setTypeface(face);
    }

    public MyanmarTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        String fontName = "fonts/OpenSans.ttf";
        String language = context
                .getSharedPreferences("org.bitbucket.infovillafoundation.denko", Context.MODE_PRIVATE)
                .getString("org.bitbucket.infovillafoundation.denko.language", "en");
        if (language.equals("my"))
            fontName = "fonts/WINNWAB.ttf";
        Typeface face = Typeface.createFromAsset(context.getAssets(), fontName);
        this.setTypeface(face);
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

}

问题是本地语言的字体大小需要大于英语的字体大小。例如,为了创建一个英文文本设置为字体大小为10的外观,本地语言文本的字体大小应为13.如果我希望字体大小由TextView自动设置,请执行我为给定的字体大小添加一个常量值,还是将它乘以一个常量?或者它是完全不同的东西?

2 个答案:

答案 0 :(得分:1)

我认为这取决于您的自定义字体WINWAB.ttf,该字体可能小于OpenSans.ttf。它实际上与android大小无关,10sp11sp不同,只是后者更大。

我建议测试尺寸并尝试在OpenSans.ttfWINWAB.ttf渲染尺寸之间找到一些关系。添加+3可能值得考虑,您很幸运已经有了自定义视图,因此很容易实现。

答案 1 :(得分:0)

我不知道我是否说得对,但试试这个 - >

this.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);

或任何您想要的东西,而不是14