动态字体大小

时间:2011-06-04 08:51:28

标签: java android

如何计算TextView的字体大小,以便始终占据屏幕的某个百分比? 现在我吐了出来:

    .....
densityNormal = Float.parseFloat("1.0");
densitySub = Float.parseFloat("0.75");
densityIper = Float.parseFloat("1.5");
densityExtra = Float.parseFloat("2.0");
.....
textviewSample.setTextSize(getHeightPercentage(context,30));
.....
private float getHeightPercentage(Context context, int percentage) {
    if (percentage > 0 && percentage < 100) {

        float appHeight = context.getApplicationContext().getResources()
                .getDisplayMetrics().heightPixels;

        float appSPDensity = context.getApplicationContext().getResources()
                .getDisplayMetrics().scaledDensity;

        if (Float.compare(appSPDensity, densitySub) == 0) {
            appHeight = appHeight + ((appHeight * 25) / 100);
        } else if (Float.compare(appSPDensity, densityIper) == 0) {
            appHeight = ((appHeight * 50) / 100) - appHeight;
        } else if (Float.compare(appSPDensity, densityExtra) == 0) {
            appHeight = ((appHeight * 200) / 100) - appHeight;
        }

        return new Float((appHeight * percentage) / 100);
    } else
        return -1;
}

但我无法整合不同密度的屏幕,你有什么建议吗?8y3

0 个答案:

没有答案