在Android的TextView中期末的英文缩写,不要换行 [英] Don't wrap text in Android TextView at period in abbreviation

查看:266
本文介绍了在Android的TextView中期末的英文缩写,不要换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来控制,其中一个TextView选择来包装它的文本?说我遇到的问题是,它要在包裹内如此 - 所以像可在美国和加拿大的字符串可以在U和S之间的期限后,包装有没有办法告诉TextView的不包装,除非有一个时间和空间,或者一些编程的方式来控制包装?

解决方案

最后我写我自己的算法只在空格符文

。我原本使用的<一个href="http://developer.android.com/reference/android/graphics/Paint.html#breakText%28java.lang.CharSequence,%20int,%20int,%20boolean,%20float,%20float%5b%5d%29">breakText 的方法涂料,但有一些问题(这实际上可能是在这个版本中解决code,但没关系)。这不是code我最好的一块,它绝对可以清理一点,但它的工作原理。由于我重写 TextView的的,我只是把这种从<一个href="http://developer.android.com/reference/android/view/View.html#onSizeChanged%28int,%20int,%20int,%20int%29">onSizeChanged方法来确保有一个有效的宽度。

私有静态无效breakManually(TextView的电视,可编辑的编辑) {     INT宽度= tv.getWidth() - tv.getPaddingLeft() - tv.getPaddingRight();     如果(宽== 0)     {         //不能为0的宽度打破。         返回false;     }     涂料P = tv.getPaint();     浮动[]宽度=新的浮动[editable.length()];     p.getTextWidths(editable.toString(),宽度);     浮curWidth = 0.0;     INT lastWSPos = -1;     INT strPos = 0;     最后的字符换行='\ N';     最后弦乐newLineStr =\ N的;     布尔复位= FALSE;     INT insertCount = 0;     //遍历从开始位置的字符串,将每个字符的     //宽度与总直至:     // *空格字符被找到。在这种情况下,标记的空白     //位置。如果宽度越过最大,这是其中的换行     //将被插入。     // *一个换行符被找到。这将重置curWidth计数器。     // * curWidth&GT;宽度。更换空白以新行和复位     //柜台。     而(strPos&LT; editable.length())     {         curWidth + =宽度[strPos]         焦炭curChar = editable.charAt(strPos);         如果(((int)的curChar)==((INT)换行))         {             复位= TRUE;         }         否则,如果(Character.isWhitespace(curChar))         {             lastWSPos = strPos;         }         否则,如果(curWidth&GT;宽度安培;&安培; lastWSPos&GT; = 0)         {             editable.replace(lastWSPos,lastWSPos + 1,newLineStr);             insertCount ++;             strPos = lastWSPos;             lastWSPos = -1;             复位= TRUE;         }         如果(重置)         {             curWidth = 0.0;             复位= FALSE;         }         strPos ++;     }     如果(insertCount!= 0)     {          tv.setText(编辑);     } }

Is there a way to control where a TextView chooses to wrap its text? The issue that I'm having is that it wants to wrap at periods - so a string like "Available in the U.S. and Canada" could wrap after the period between the U and the S. Is there a way to tell the TextView not to wrap unless there's a period and a space, or some programmatic way to control the wrapping?

解决方案

I ended up writing my own algorithm to break the text only on whitespace. I had originally used the breakText method of Paint, but was having some issues (that may actually be resolved in this version of the code, but that's OK). This isn't my best chunk of code, and it could definitely be cleaned up a bit, but it works. Since I'm overriding TextView, I just call this from the onSizeChanged method to ensure that there's a valid width.

private static void breakManually(TextView tv, Editable editable)
{
    int width = tv.getWidth() - tv.getPaddingLeft() - tv.getPaddingRight();
    if(width == 0)
    {
        // Can't break with a width of 0.
        return false;
    }

    Paint p = tv.getPaint();
    float[] widths = new float[editable.length()];
    p.getTextWidths(editable.toString(), widths);
    float curWidth = 0.0f;
    int lastWSPos = -1;
    int strPos = 0;
    final char newLine = '\n';
    final String newLineStr = "\n";
    boolean reset = false;
    int insertCount = 0;

    //Traverse the string from the start position, adding each character's
    //width to the total until:
    //* A whitespace character is found.  In this case, mark the whitespace
    //position.  If the width goes over the max, this is where the newline
    //will be inserted.
    //* A newline character is found.  This resets the curWidth counter.
    //* curWidth > width.  Replace the whitespace with a newline and reset 
    //the counter.

    while(strPos < editable.length())
    {
        curWidth += widths[strPos];

        char curChar = editable.charAt(strPos);

        if(((int) curChar) == ((int) newLine))
        {
            reset = true;
        }
        else if(Character.isWhitespace(curChar))
        {
            lastWSPos = strPos;
        }
        else if(curWidth > width && lastWSPos >= 0)
        {
            editable.replace(lastWSPos, lastWSPos + 1, newLineStr);
            insertCount++;
            strPos = lastWSPos;
            lastWSPos = -1;
            reset = true;
        }

        if(reset)
        {
            curWidth = 0.0f;
            reset = false;
        }

        strPos++;
    }

    if(insertCount != 0)
    {
         tv.setText(editable);
    }
}

这篇关于在Android的TextView中期末的英文缩写,不要换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆