我可以限制 TextView 的字符数吗?

发布于 2025-01-02 19:37:40 字数 159 浏览 4 评论 0原文

我现在拥有的是 TextView 元素的 ListView。每个 TextView 元素显示一个文本(文本长度从 12 个单词到 100+ 不等)。我想要的是让这些 TextView 显示文本的一部分(假设 20 个单词或大约 170 个字符)。

如何将TextView限制为固定字符数?

What I have now is a ListView of TextView elements. each TextView element displays a text (the text length varies from 12 words to 100+). What I want is to make these TextViews display a portion of the text (let's say 20 word or roughly 170 chars).

How to limit the TextView to a fixed number of characters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

極樂鬼 2025-01-09 19:37:40

这是一个例子。我使用 maxLength 属性限制大小,使用 maxLines 属性将其限制为单行,然后使用 ellipsize=end 自动将“...”添加到已被截断的任何行的末尾。

<TextView 
    android:id="@+id/secondLineTextView" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:maxLines="1" 
    android:maxLength="10" 
    android:ellipsize="end"/>

Here is an example. I limit the sizewith the maxLength attribute, limit it to a single line with maxLines attribute, then use the ellipsize=end to add a "..." automatically to the end of any line that has been cut-off.

<TextView 
    android:id="@+id/secondLineTextView" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:maxLines="1" 
    android:maxLength="10" 
    android:ellipsize="end"/>
贱贱哒 2025-01-09 19:37:40

如果您对 xml 解决方案不感兴趣,也许您可​​以这样做:

String s="Hello world";
Textview someTextView;
someTextView.setText(getSafeSubstring(s, 5));
//the text of someTextView will be Hello

...

public String getSafeSubstring(String s, int maxLength){
  if(!TextUtils.isEmpty(s)){
    if(s.length() >= maxLength){
      return s.substring(0, maxLength);
    }
  }
  return s;
}

If your not interested in xml solutions maybe you can do this:

String s="Hello world";
Textview someTextView;
someTextView.setText(getSafeSubstring(s, 5));
//the text of someTextView will be Hello

...

public String getSafeSubstring(String s, int maxLength){
  if(!TextUtils.isEmpty(s)){
    if(s.length() >= maxLength){
      return s.substring(0, maxLength);
    }
  }
  return s;
}
初与友歌 2025-01-09 19:37:40

在 TextView 中使用以下代码

 android:maxLength="65"

享受...

Use below code in TextView

 android:maxLength="65"

Enjoy...

从﹋此江山别 2025-01-09 19:37:40

我使用 maxEms 属性来完成此操作。

 <TextView
    android:ellipsize="end"
    android:maxEms="10"/>

I did this using the maxEms attribute.

 <TextView
    android:ellipsize="end"
    android:maxEms="10"/>
复古式 2025-01-09 19:37:40

我正在分享一个示例,其中我设置了 maxLength=1 即将其限制为具有 maxLines 属性的单行,然后使用 ellipsize=end 自动将“...”添加到已被截断的任何行的末尾。

请注意:layout_width 为 120dp,即任何文本在 120dp 之后
超过将触发“ellipsize=end”属性

直接粘贴下面的代码进行检查。

<TextView
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:maxLength="40"
    android:text="Can I limit TextView's number of characters?"
    android:textColor="@color/black"
    android:textSize="12sp"
    android:textStyle="bold" />

I am sharing an example where I have set maxLength=1 i.e. limit it to a single line with maxLines attribute, then use the ellipsize=end to add a "..." automatically to the end of any line that has been cut-off.

Please Note: layout_width which is 120dp i.e. after 120dp any text
exceeding will triggrer "ellipsize=end" property

paste the below code directly to check.

<TextView
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:maxLength="40"
    android:text="Can I limit TextView's number of characters?"
    android:textColor="@color/black"
    android:textSize="12sp"
    android:textStyle="bold" />

.

孤寂小茶 2025-01-09 19:37:40

程序化 Kotlin。

截断文本的开头:

 val maxChars = 10000
 if (helloWorldTextView.text.length > maxChars) {
      helloWorldTextView.text = helloWorldTextView.text.takeLast(maxChars)
 }

截断文本的结尾:

 val maxChars = 10000
 if (helloWorldTextView.text.length > maxChars) {
      helloWorldTextView.text = helloWorldTextView.text.take(maxChars)
 }

Programmatic Kotlin.

Cut off the start of the text:

 val maxChars = 10000
 if (helloWorldTextView.text.length > maxChars) {
      helloWorldTextView.text = helloWorldTextView.text.takeLast(maxChars)
 }

Cut off the end of the text:

 val maxChars = 10000
 if (helloWorldTextView.text.length > maxChars) {
      helloWorldTextView.text = helloWorldTextView.text.take(maxChars)
 }
骄兵必败 2025-01-09 19:37:40

这是正确的解决方案。请记住,将宽度设置为 WRAP CONTENT。 ems 不是基于 Max Charater 的。 EMS 基于宽度。如果 ems 值宽度超过文本长度,则自动 (...) 将添加到 textview 的最后。

android:ems="8"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This is a demo text"

Here is the correct Solution. Keep remember, to set width to WRAP CONTENT. and ems is not based on Max Charater. EMS based on width. If ems value width will exceed the text length, then automatically (...) will be added at last of textview.

android:ems="8"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This is a demo text"
揪着可爱 2025-01-09 19:37:40

正如 https://stackoverflow.com/a/6165470/1818089 中提到的https://stackoverflow.com/a/6239007/1818089,使用

android:minEms="2"

应该足以实现上述目标。

As mentioned in https://stackoverflow.com/a/6165470/1818089 & https://stackoverflow.com/a/6239007/1818089, using

android:minEms="2"

should be enough to achieve the goal stated above.

讽刺将军 2025-01-09 19:37:40

您可以扩展 TextView 类并覆盖 setText() 函数。
在此功能中,您可以检查文本长度或单词数量。

you can extend the TextView class and overwrite the setText() function.
In this function you check for text length or word cound.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文