【问题标题】:get width of textview in characters以字符为单位获取 textview 的宽度
【发布时间】:2012-07-14 21:12:17
【问题描述】:

我认为这是相反的 Set width of TextView in terms of characters

我有一个 TextView,我在其中显示一些报告数据。我对其中的一部分使用等宽 TypefaceSpan,因为我希望列对齐。

我使用我的测试 Android 设备来计算我可以容纳多少列,但 Android 模拟器似乎正好少了一列,这使得在纵向模式下以一种丑陋的方式包装。

有没有办法找出一行应该容纳多少个字符?

【问题讨论】:

    标签: android layout


    【解决方案1】:

    答案是使用 textView 的 Paint 对象的 breakText()。这是一个示例,

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

    现在totalCharstoFit 包含可以放入一行的确切字符。现在您可以创建一个完整字符串的子字符串并将其附加到 TextView 像这样,

    String subString=fullString.substring(0,totalCharstoFit);
    textView.append(substring);
    

    并且要计算剩余的字符串,可以这样做,

    fullString=fullString.substring(subString.length(),fullString.length());
    

    现在是完整的代码,

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

    【讨论】:

    • while 循环中的错字:.append(substring) 应该大写 S:.append(subString)
    【解决方案2】:

    你可以做数学来找出这个,找到字符的宽度,然后将屏幕的宽度除以这个,你就会得到你要找的东西。

    但是不能设计得更好吗?有没有可以组合在一起的列?显示为图形,甚至完全排除?

    另一种可能的解决方案是使用类似 viewpager 的东西。 (找出有多少列的宽度适合第一页,然后将剩余的表格拆分到第二页。

    【讨论】:

    【解决方案3】:

    您可以通过下面的代码获取Textview的总行并为每个字符获取字符串。然后您可以为每一行设置任何您想要的样式。

    我将第一行设置为粗体。

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

    试试这个方法。你可以用这种方法应用多种样式。

    您还可以通过以下方式获取 textview 的宽度:

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

    【讨论】:

      猜你喜欢
      • 2011-03-12
      • 2013-06-17
      • 1970-01-01
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 2017-11-21
      • 2012-07-18
      • 1970-01-01
      相关资源
      最近更新 更多