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).
我现在有一个TextView元素的ListView。每个TextView元素都显示一个文本(文本长度从12个单词到100+不等)。我想要的是让这些textview显示文本的一部分(比如20个单词或大约170个字符)。
How to limit the TextView to a fixed number of characters?
如何限制TextView的字符数?
6 个解决方案
#1
128
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.
这是一个例子。我将sizewith maxLength属性限制为只使用maxLines属性的一行,然后使用椭圆体=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"/>
#2
4
Use below code in TextView
在TextView中使用下面的代码。
android:maxLength="65"
Enjoy...
享受……
#3
4
If your not interested in xml solutions maybe you can do this:
如果您对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;
}
#4
1
You can use setEllipsize method of TextView class http://developer.android.com/reference/android/widget/TextView.html#setEllipsize(android.text.TextUtils.TruncateAt)
可以使用TextView类的set椭圆体方法http://developer.android.com/reference/android/widget/TextView.html# set椭圆体(android.text.TextUtils.TruncateAt)
With the constants of TextUtil class for added the suspension points http://developer.android.com/reference/android/text/TextUtils.TruncateAt.html
使用TextUtil类的常量添加悬浮点http://developer.android.com/reference/android/text/TextUtils.TruncateAt.html
#5
0
As mentioned in https://*.com/a/6165470/1818089 & https://*.com/a/6239007/1818089, using
如https://*.com/a/6165470/1818089和https://*.com/a/6239007/1818089所述。
android:minEms="2"
should be enough to achieve the goal stated above.
应足以实现上述目标。
#6
-5
you can extend the TextView class and overwrite the setText() function. In this function you check for text length or word cound.
可以扩展TextView类并覆盖setText()函数。在这个函数中,检查文本长度或单词数量。
#1
128
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.
这是一个例子。我将sizewith maxLength属性限制为只使用maxLines属性的一行,然后使用椭圆体=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"/>
#2
4
Use below code in TextView
在TextView中使用下面的代码。
android:maxLength="65"
Enjoy...
享受……
#3
4
If your not interested in xml solutions maybe you can do this:
如果您对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;
}
#4
1
You can use setEllipsize method of TextView class http://developer.android.com/reference/android/widget/TextView.html#setEllipsize(android.text.TextUtils.TruncateAt)
可以使用TextView类的set椭圆体方法http://developer.android.com/reference/android/widget/TextView.html# set椭圆体(android.text.TextUtils.TruncateAt)
With the constants of TextUtil class for added the suspension points http://developer.android.com/reference/android/text/TextUtils.TruncateAt.html
使用TextUtil类的常量添加悬浮点http://developer.android.com/reference/android/text/TextUtils.TruncateAt.html
#5
0
As mentioned in https://*.com/a/6165470/1818089 & https://*.com/a/6239007/1818089, using
如https://*.com/a/6165470/1818089和https://*.com/a/6239007/1818089所述。
android:minEms="2"
should be enough to achieve the goal stated above.
应足以实现上述目标。
#6
-5
you can extend the TextView class and overwrite the setText() function. In this function you check for text length or word cound.
可以扩展TextView类并覆盖setText()函数。在这个函数中,检查文本长度或单词数量。