2011-12-01 43 views
0

我有一個主頁和頁眉,頁腳之間,我有10行文本顯示。在java中的文本大小是硬編碼,但是當我運行該應用程序頁腳得到拉伸調整與固定的文本大小。但它在伸展時看起來很奇怪。有沒有一種方法可以根據屏幕動態分配文本大小。請幫助我,如果你可以看看圖像,你會明白頁腳問題。如何動態修復不同屏幕分辨率的文字大小Android

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

嘗試使用sp單位代替文本大小的px。 – Warpzit

+0

@Pink任何機會,你是一樣的[人這個](http://stackoverflow.com/users/991423/pink-candy)? – Reno

+0

是的,我出於某種原因無法訪問該帳戶,這就是爲什麼創建一個新帳戶。對不起 – cavallo

回答

2

嘗試設置你的文字大小在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); 
    } 

希望它幫你!