简体   繁体   中英

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

i have a home screen with header and footer and in between i have ten rows of text to display. the text size is hard coded in java but when i run the app the footer gets stretched to adjust with the text size fixed. but it looks odd when it gets stretched. is there a way to assign the text size dynamically based on the screen. please help me with this, if you could look at the image you will understand the footer problem.

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));
    }

Try to set your text size in DP and dynamically convert DP to PX and put this PX values in

*.textSize(pxValue);

To convert I use next Code:

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);
    }

Hope it help you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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