2013-09-23 76 views

回答

0

願這可以幫助你

// Determine Scaled Density for later calculations 
Display dd = ((Activity) m_Context).getWindowManager().getDefaultDisplay(); 
DisplayMetrics dm = new DisplayMetrics(); 
dd.getMetrics(dm); 
float m_ScaledDensity = dm.scaledDensity; 

Rect bounds = new Rect(); 
Paint p = new Paint(); 
p.setTypeface(btn.getTypeface()); 

// W is a very wide character 
String sample = "NeedsToFIt" 
int maxFont; 
for (maxFont= 1 
    ; -bounds.top <= btn.getHeight() && bounds.right <= btn.getWidth() 
    ; maxFont++) { 
    p.setTextSize(maxFont); 
    p.getTextBounds(sample, 0, sample.length(), bounds); 
} 
maxFont = (int) ((maxFont - 1)/m_ScaledDensity); 
btn.setTextSize(maxFont); 
+0

它沒有工作,所以我調試它和btn.getHeight()爲0,也許是因爲我的按鈕使用wrap_content,它會自動調整它的資源維度,我也不明白你爲什麼要除以m_ScaledDensity在btn.setTextSize而不是在p.setTextsize處進行分割。 –