我正在開發一個android應用程序,我需要知道可以在一行中顯示的字符數,以便用某種方法確定我的字符串的行數。
這怎麼辦? 坦克很多。如何知道可以在一行中放置多少個字符?
回答
這應該爲你做的工作(給或不使用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;
}
..你的想法; - )
坦克的寬度大很多,這是有效的:D – LMCompany
每個角色都有自己的寬度。 由於任何包含10個字符的字符串與也包含10個字符的另一個字符串不同。
你可以通過 - > textView.setTypeface(Typeface.MONOSPACE)來設置它。
等寬字體用於製作將定期字符等寬。 然後你可以做任何事情,以確定可以在一行中放置多少個字符?
嗯,我使用阿拉伯字符串,並使用字體作爲字體,我怎麼能做到這一點? – LMCompany
你可以試試這個
設置Typeface.MONOSPACE爲GEET建議。然後執行以下操作:
Paint.measureText(String s)返回String的寬度。通過使用這個,你可以獲得所需的1個字符的寬度。從View.getwidth()方法你可以得到視圖的寬度。所以從這兩個值就可以計算出
試試這個:
int totalLine = textView.getMeasuredHeight()/textView.getLineHeight();
- 1. 我可以在控件中放置多少個字符,WP7 Silverlight
- 2. android java - 如何知道有多少個字符留在一行的末尾?
- 3. 如何知道我的文字是多少行包一個DIV
- 4. 有多少個字符可以放入C++字符串中?
- 5. Python的IDE可以在一行中使用多少個字符?
- 6. 計算有多少個字符可以放在UILabel的一行上
- 7. 可以爲字符串變量設置多少個字符?
- 8. Mapper可以知道有多少Mapper在運行嗎?
- 9. 如何 「免費」 知道它有多少字節釋放
- 10. 我如何可以設置多少個字符可以輸入的限制? (JAVA)
- 11. 知道有多少行有一個按鈕'文字android
- 12. 有什麼方法可以知道NSUserNotificationCenter中有多少個NSUserNotifications?
- 13. 一行可以包含多少個字符?(C#打印文本)
- 14. 我想知道有多少訂單,每個客戶的放置
- 15. 如何在POSTMAN中放置多行單個字符串JSON?
- 16. 您可以將多少個字符擠入Apple推送通知?
- 17. 如何知道可以產生多少短路哈希?
- 18. 有多少個UITableView可以放置在視圖中?
- 19. 找出有多少個字符可以放進DIV的JavaScript
- 20. 我可以在一個字符串中放置圓括號嗎?
- 21. 您可以在一個Windows Azure存儲帳戶中放置多少個表?
- 22. 如何知道在NSMatrix中選擇了多少行和列?
- 23. 如何知道如果一個字符串包含在Java中
- 24. 如何知道閃爍的管道字符在一個字符串中的哪個位置?
- 25. 有可能知道一個AVFrame有多少個AVPackets?
- 26. 優化器,如何一個方法可以知道有多少參數在另一個
- 27. 一次可以運行多少個AVD?
- 28. 如何知道段落在MySQL中包含多少個單詞?
- 29. 我如何知道在XML字段中放置什麼?
- 30. 如何知道一個python程序在運行時執行多少個字節碼
你在說什麼? 'Edittext','TextView' ????? – 7bluephoenix
TextView,但我使用LayoutInflater,所以我不能使用getlinecount – LMCompany