得到的TextView的字符宽度 [英] get width of textview in characters
问题描述
我觉得这是相反的 TextView的在人物方面设置宽度
I think this is the opposite of Set width of TextView in terms of characters
我在这里我展示了一些报告的数据一个TextView。我使用的是等宽TypefaceSpan为它的一部分,因为我想列排队。
I have a TextView where I'm showing some report data. I use a monospace TypefaceSpan for a portion of it because I want columns to line up.
我用我的测试Android设备找出多少列,我可以适应,但Android模拟器似乎有一个确切的少列,这使得事情包装在一个丑陋的方式在纵向模式下。
I used my test Android device to figure out how many columns I could fit, but the Android emulator seems to have exactly one less column, which makes things wrap in an ugly way in portrait mode.
有没有办法找出多少个字应该适合在1号线?
Is there a way to find out how many characters should fit on 1 line?
推荐答案
答案是使用你的TextView的Paint对象的breakText()。这里是一个示例,
The answer would be to use the breakText() of your textView's Paint Object. Here is a Sample ,
int totalCharstoFit= textView.getPaint().breakText(fullString, 0, fullString.length(),
true, textView.getWidth(), null);
现在 totalCharstoFit
包含准确的字符,可以配入一行。而现在你可以让你满弦的子字符串,并将其追加到像这样的TextView,
Now totalCharstoFit
contains the exact characters that can be fit into one line. And now you can make a sub string of your full string and append it to the TextView like this,
String subString=fullString.substring(0,totalCharstoFit);
textView.append(substring);
和计算剩余的字符串,你可以做到这一点,
And to calculate the remaining string, you can do this,
fullString=fullString.substring(subString.length(),fullString.length());
现在满code,
在while循环做到这一点,
Do this in a while loop,
while(fullstirng.length>0)
{
int totalCharstoFit= textView.getPaint().breakText(fullString, 0, fullString.length(),
true, textView.getWidth(), null);
String subString=fullString.substring(0,totalCharstoFit);
textView.append(substring);
fullString=fullString.substring(subString.length(),fullString.length());
}
这篇关于得到的TextView的字符宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!