【问题标题】:How to count the number of letters that you see in TextView?如何计算您在 TextView 中看到的字母数量?
【发布时间】:2017-04-27 14:44:59
【问题描述】:

全部。我是android开发的新手。现在我正在使用 TextView。视情况而定,我设置了不同的文本。但有时文本非常大。例如,我插入这个“abcdefgh_myword”,我在我的小 TextView 中得到了类似“abcdefg”的东西,但我想得到这个“..._myword”。所以我想计算我看到的字母数量(程序)并设置我想看到的文本。这个怎么做?有什么想法吗?

....

android:ellipsize="start" 无法按我的意愿工作...请注意当我限制 TextView 的宽度时会发生什么

之前

之后

<TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:ellipsize="start"
                android:gravity="center"
                android:maxLines="1"
                android:text="abcdef1234567" />

有什么想法吗?

【问题讨论】:

标签: java android textview android-widget


【解决方案1】:

这对你有帮助

android:ellipsize="start"
android:maxLines="1"

将其添加到 XML 文件中的 TextView

编辑:

据此:https://stackoverflow.com/a/39890995/7325737 你必须使用android:singleLine="true",因为maxLines只支持endmarquee

【讨论】:

  • 谢谢,比 --android:maxLines="1"--
【解决方案2】:

这只有在您知道每个符号的宽度时才有可能。您通常不会这样做,因为字体取决于系统。

您可以为此使用等宽字体。

【讨论】:

    【解决方案3】:

    除了 lidkxx 的答案之外,您还可以在您的文本视图上调用 getText() 来获取字符串。

    TextView textview = (TextView) findViewById(R.id.your_text_view);
    String s = textview.getText();
    int letterCount = s.length();
    

    【讨论】:

      【解决方案4】:

      您可能正在寻找Java 的字符串length() 方法。检查this 答案。然后根据需要在 XML 中使用 android:ellipsize="start"setEllipsize(TruncateAt.START);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 2020-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-08
        相关资源
        最近更新 更多