如何查找每行的 android TextView 字符数? [英] How to find android TextView number of characters per line?

查看:24
本文介绍了如何查找每行的 android TextView 字符数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在 android 中有一个 TextView,其宽度与屏幕的整个长度相同,并且填充为 dip 5.如何计算适合屏幕上单行的字符数?我想换句话说,我正在尝试获取 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 和 width 手动计算,但是 1)不知道相关性和 2)由于 dip 为单位的填充,不同的屏幕将使用不同数量的实际像素来填充.

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

我使用上面的代码生成了一个原始字符串 s,一个将被打印的字符串 u.但是,我认为这种方法非常低效.是否有另一种方法或更好的算法?注意: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 的内容应该是特定行中的所有字符.如果为真,则开始新行(并使用新字符串作为参数传递).

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).

回复评论:

  1. 因为该应用可以在许多系统中运行,这正是您需要衡量它的原因.
  2. 这是解决您的整体问题"的一种方法.使用 str.size()>numCol vs is too large 有什么区别?您需要实现动画(提示 #1:插入换行符)
  3. 正如我之前所说,当你开始一个新行时,你开始一个新字符串(提示 #2:如果你扩展 TextView,你可以在覆盖 setText 中实现所有这些).(提示 #3:跟踪使用 static int lines; 创建的行并使用 newString.split("\r?\n")[lines-1] 检查长度).
  1. because the app can be run in many systems is exactly why you need to measure it.
  2. This is a way to solve your "overall question". What is the difference between using str.size()>numCol vs is too large? You will need to implement your animation (hint #1: insert a newline character)
  3. 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).

这篇关于如何查找每行的 android TextView 字符数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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