如何找到每行的字符的android TextView的数量?字符、数量、android、TextView

2023-09-12 09:18:28 作者:晚街听风

所以,我在android的,有屏幕的整个长度的宽度和倾角5的填充一个TextView如何计算的字符,将适合在屏幕上一行是多少?我想,换句话说,我试图让一个TextView的列数?

So I have a TextView in android that has the width of the whole length of the screen and a padding of dip 5. How can I calculate the number of characters that will fit a single line on the screen? I guess in other words, I'm trying to get the number of columns of a textview?

我认为这取决于TEXTSIZE和宽度的人工计算,但1)不知道的相关性; 2)由于浸的单位填充,不同的屏幕将使用不同数量的实际像素填充的。

I considered manual calculation depending on textsize and width, but 1) don't know the correlation and 2) due to the padding in the units of dip, different screens will use different number of actual pixels to pad.

总体问:我想用这个来解决:如果给一个字符串,我怎么可以手动编辑字符串,这样当TextView的打印由字符字符串的字符,我就知道什么时候开始一个字,不会配合上的下一行。注:我知道的TextView会自动将,将不适合到下一行的话,但是,因为我打印字符的字符,如打字动画,TextView的不知道这个词不适合直到它打印出这个词四溢的字符。

Overall Question: I am trying to use this to solve: if given a string how can I manually edit to string such that when the textview prints the string character by character, I will know when to start a word that won't fit on one line on the next. Note: I know that textview automatically puts words that won't fit onto the next line, however, since I'm printing character by character, like typing animation, textview doesn't know the word won't fit until it prints out the overflowing characters of that word.

一直四处寻找这...

Been searching everywhere for this...

谢谢!

补充解决方案:

一个可能的解决方案:

public String measure2 (TextView t, String s) {
    String u = "";
    int start = 0;
    int end = 1;
    int space = 0;
    boolean ellipsized = false;
    float fwidth = t.getMeasuredWidth();
    for(;;) {
        //t.setText(s.substring(start, end));
        float twidth = t.getPaint().measureText(s.substring(start, end));
        if (twidth < fwidth){
            if (end < s.length())
                end++;
            else {
                if (!ellipsized)
                    return s;
                return u + s.subSequence(start, end);
            }
        }
        else {
            ellipsized = true;
            space = (u + s.substring(start, end)).lastIndexOf(" ");
            if (space == -1)
                space = end - 1;
            u += s.subSequence(start, space) + "\n";
            start = space + 1;
            end = start + 1;
        }
    }
}

2的解决方案,但仍使用解决方法1,有时:

solution 2, but still uses solution1 sometimes:

public String measure3 (TextView t, String s) {
    List<String> wlist = Arrays.asList(s.split(" "));
    if (wlist.size() == 1)
        return measure2(t, s);
    String u = "";
    int end = 1;
    float fwidth = t.getMeasuredWidth();
    for(;;) {
        //t.setText(s.substring(start, end));
        if (wlist.isEmpty())
            return u;
        String temp = listStr(wlist, end);
        float twidth = t.getPaint().measureText(temp);
        if (twidth < fwidth){
            if (end < wlist.size())
                end++;
            else {
                return u + temp;
            }
        }
        else {
            temp = listStr(wlist, end-1);
            if (end == 1)
                temp = measure2(t, temp);
            if (wlist.isEmpty())
                return u + temp;
            else
                u = u + temp + "\n";
            wlist = wlist.subList(end - 1, wlist.size());
            end = 1;
        }
    }
}

public String listStr (List<String> arr, int end) {
    String s = "";
    for (String e : arr.subList(0, end) ){
        s = s + e + " ";
    }
    return s.trim();
}

我用上面的code产生掀起了原来的字符串s,字符串ü将被打印出来。但是,我认为这种做法是非常低效的。是否有另一种方法或更好的算法?注意:有在measure3,我修正了一些错误,但懒得修改

I used the above code to generate off a original string s, a string u that would be printed. However, I think this approach is very inefficient. Is there another approach or a better algorithm? Note: there are some errors in measure3 that I fixed, but was too lazy to edit

推荐答案

试试这个:

private boolean isTooLarge (TextView text, String newText) {
    float textWidth = text.getPaint().measureText(newText);
    return (textWidth >= text.getMeasuredWidth ());
}

检测的字符如何配合将是不可能的,由于字符的可变宽度。上述功能将测试如果一个特定的字符串将适合或不能在TextView中。 newText的内容应该是在一个特定的行中的所有字符。如果为true,则开始新行(和使用新的字符串作为参数传递)。

Detecting how many characters fit will be impossible due to the variable width of the characters. The above function will test if a particular string will fit or not in the TextView. The content of newText should be all the characters in a particular line. If true, then start a new line (and using a new string to pass as parameter).

答到注释:

,因为应用程序可以在许多系统上运行的是到底为什么你需要衡量它。 这是一个方法来解决你的整体问题。使用str.size()> numCol VS isTooLarge之间的区别是什么?你将需要实现你的动画(提示#1:插入一个换行符) 在前面说过,当你开始一个新行,你开始一个新的字符串(提示#2:如果你扩展的TextView,你可以实现这一切,压倒一切的setText )。 (提示#3:跟踪与静态INT线创建的线; 和使用 newString.split(\\ \\řñ )[线路-1] 来检查长度)。 because the app can be run in many systems is exactly why you need to measure it. This is a way to solve your "overall question". What is the difference between using str.size()>numCol vs isTooLarge? You will need to implement you animation (hint #1: insert a newline character) as I said before, when you start a new line, you start a new string (hint #2: if you extend TextView, you can implement all this in overriding setText). (hint #3: Keep track of the lines created with a static int lines; and use newString.split("\\r?\\n")[lines-1] to check for length).