繁体   English   中英

如何针对android上的不同屏幕分辨率动态修复文本大小

[英]how to fix the text size dynamically for different screen resolutions on android

我有一个带页眉和页脚的主屏幕,在它之间我有十行文字要显示。 文本大小在java中是硬编码的,但是当我运行应用程序时,页脚会被拉伸以调整文本大小。 但是当它被拉伸时看起来很奇怪。 有没有办法根据屏幕动态分配文本大小。 请帮我这个,如果你能看一下你会理解页脚问题的图像。

if (dpiClassification == DisplayMetrics.DENSITY_HIGH) {
        if (isTablet) {
            textSize = (int) (((d.getHeight() - (totImageHeight)) / 10) - (25 * dm.density));
        } else {
            textSize = (int) (((d.getHeight() - (totImageHeight)) / 10) - (11 * dm.density));
        }

    } else if (dpiClassification == DisplayMetrics.DENSITY_MEDIUM) {
        if (isTablet) {
            textSize = 90;// (int) (((d.getHeight() - (totImageHeight)) /
                            // 10) - (15 * dm.density));
        } else {
            textSize = (int) (((d.getHeight() - (totImageHeight)) / 10));
        }
    } else {
        textSize = (int) (((d.getHeight() - (totImageHeight)) / 10));
    }

尝试在DP中设置文本大小并动态地将DP转换为PX并将此PX值放入

*.textSize(pxValue);

要转换我使用下一个代码:

private static DisplayMetrics metrics = null;
private static float density = 0.0;

private int dpToPxConverter(Context context, int dp){
        if (metrics == null) {
            metrics = context.getResources().getDisplayMetrics();
            density = metrics.density;
        }
        return (int) (density * dp + 0.5f);
    }

希望对你有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM