简体   繁体   English

以字符获取textview的宽度

[英]get width of textview in characters

I think this is the opposite of Set width of TextView in terms of characters 我认为这与TextView的字宽设置相反

I have a TextView where I'm showing some report data. 我有一个TextView,我正在显示一些报告数据。 I use a monospace TypefaceSpan for a portion of it because I want columns to line up. 我使用等宽字体TypefaceSpan作为其中的一部分,因为我想要列排列。

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. 我使用我的测试Android设备来计算出我可以容纳多少列,但Android模拟器似乎只有少了一列,这使得事物在纵向模式下以难看的方式包裹。

Is there a way to find out how many characters should fit on 1 line? 有没有办法找出一行中应该有多少个字符?

The answer would be to use the breakText() of your textView's Paint Object. 答案是使用textView的Paint对象的breakText()。 Here is a Sample , 这是一个样本,

int totalCharstoFit= textView.getPaint().breakText(fullString,  0, fullString.length(), 
 true, textView.getWidth(), null);

Now totalCharstoFit contains the exact characters that can be fit into one line. 现在totalCharstoFit包含可以放入一行的确切字符。 And now you can make a sub string of your full string and append it to the TextView like this, 现在,您可以创建完整字符串的子字符串,并将其附加到TextView,如下所示,

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());

Now the full code, 现在完整的代码,

Do this in a while loop, 在while循环中执行此操作,

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());

}

Well you could do math to find this out, find the width of the character, divide the width of the screen by this, and you'd have what you're looking for. 好吧,你可以做数学找出这个,找到角色的宽度,用这个划分屏幕的宽度,你就得到了你想要的东西。

But is it not possible to design it better? 但是不可能更好地设计它吗? Are there any columns you can group together? 您可以将任何列组合在一起吗? Display as a graphic, or even exclude completely? 显示为图形,甚至完全排除?

Another possible solution is to use something like a viewpager. 另一种可能的解决方案是使用类似viewpager的东西。 (Find out how many columns' width fit on the first page, and then split the remaining tables onto the second page). (找出第一页上有多少列的宽度,然后将剩余的表分成第二页)。

You can get total line of Textview and get string for each characters by below code.Then you can set style to each line whichever you want. 您可以通过以下代码获取Textview的总行数并获取每个字符的字符串。然后您可以将样式设置为您想要的每一行。

I set first line bold. 我设置第一行粗体。

private void setLayoutListner( final TextView textView ) {
    textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            final Layout layout = textView.getLayout();

            // Loop over all the lines and do whatever you need with
            // the width of the line
            for (int i = 0; i < layout.getLineCount(); i++) {
                int end = layout.getLineEnd(0);
                SpannableString content = new SpannableString( textView.getText().toString() );
                content.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, end, 0);
                content.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), end, content.length(), 0);
                textView.setText( content );
            }
        }
    });
}

Try this way.You can apply multiple style this way. 试试这种方式。你可以用这种方式应用多种风格。

You can also get width of textview by: 您还可以通过以下方式获取textview的宽度:

for (int i = 0; i < layout.getLineCount(); i++) {
        maxLineWidth = Math.max(maxLineWidth, layout.getLineWidth(i));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM