2013-06-29 20 views

回答

1

這應該爲你做的工作(給或不使用IDE編碼採取了一些錯誤: - /)

int countLineBreaks(final TextView view, final String toMeasure) { 

    final Paint paint = textView.getPaint(); // Get the paint used by the TextView 
    int startPos = 0; 
    int breakCount = 0; 
    final int endPos = toMeasure.length(); 

    // Loop through the string, moving along the number of characters that will 
    // fit on a line in the TextView. The number of iterations = the number of line breaks 

    while (startPos < endPos) { 
     startPos += paint.breakText(toMeasure.substring(startPos, endPos), 
            true, tv.getWidth(),(float[]) null); 
     lineCount++; 
    } 
    // Line count will now equal the number of line-breaks the string will require 
    return lineCount; 
} 

..你的想法; - )

+0

坦克的寬度大很多,這是有效的:D – LMCompany

0

每個角色都有自己的寬度。 由於任何包含10個字符的字符串與也包含10個字符的另一個字符串不同。

你可以通過 - > textView.setTypeface(Typeface.MONOSPACE)來設置它。

等寬字體用於製作將定期字符等寬。 然後你可以做任何事情,以確定可以在一行中放置多少個字符?

+0

嗯,我使用阿拉伯字符串,並使用字體作爲字體,我怎麼能做到這一點? – LMCompany

0

你可以試試這個

設置Typeface.MONOSPACE爲GEET建議。然後執行以下操作:

Paint.measureText(String s)返回String的寬度。通過使用這個,你可以獲得所需的1個字符的寬度。從View.getwidth()方法你可以得到視圖的寬度。所以從這兩個值就可以計算出

+0

我設置了我的android:textSize = 4mm,是不是每個字符都具有相同的大小? – LMCompany

+0

不,它取決於字體......如果你不使用等寬字體,那麼它可以是不同的。例如0的寬度比1 – stinepike

-1

試試這個:

int totalLine = textView.getMeasuredHeight()/textView.getLineHeight(); 
相關問題