簡體   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