Mis*_*pov 2 android text textview ellipsize
我有一个 TextView,最多 15 个字符,最后它应该有三个点。使用我的 XML,它减少了 15 个字符,但最后没有添加三个点...。
重要提示:我知道当我用行数限制文本时它会起作用 -
android:maxLines="2"。但我需要用最大字符数来限制它,如问题所述。
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:maxLength="15"
android:ellipsize="end"
tools:text="123456789qwertyuiopasdfghjklzxcvbnm"
android:textSize="50sp"
/>
Run Code Online (Sandbox Code Playgroud)
结果:
我不确定如何或是否可以在 XML 中做你想做的事。
至少我不知道如何 - 但我有一个替代解决方案。
public void ellipsizeText(TextView tv){
int maxLength = 13;
if(tv.getText().length() > maxLength){
String currentText = tv.getText().toString();
String ellipsizedText = currentText.substring(0, maxLength - 3) + "...";
tv.setText(ellipsizedText);
}
}
Run Code Online (Sandbox Code Playgroud)
所以我只是把它作为一种将 textview 作为参数的方法。这真的很简单,您将 maxLength 更改为所需的字符串最大长度。在这种情况下,它被设置为 13,因为它将最后 3 个字符替换为“...”,然后产生一个仍然是 13 个字符长的字符串,但前 10 个字符是实际文本,最后 3 个字符是“ ……”。
在您的主项目中,您将 maxLength 更改为 150(或 153,如果您想要字符串中的 150 个可见字符)
希望你可以使用这个,如果你有任何问题,请告诉我。
| 归档时间: |
|
| 查看次数: |
2262 次 |
| 最近记录: |