我想限制 TextView 中的最大字数而不是最大字符数。
我的安卓代码是这样的:
<TextView
android:id="@+id/orgSummary_desc_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="10dp"
android:gravity="top"
android:maxLength="100"
android:textSize="@dimen/subtitle_text"
android:autoLink="web"
/>
我在我的 Activity 课上设置了一个长文本,结果我得到的是一些字符没有被切掉。相反,我想要这个词。
最佳答案
试试这个:
final int MAX_COUNT = 4;
TextView textView = /* your text view */
String text = "your long text comes here";
String[] segments = text.split(" ");
int wordsCount = segments.length;
if(wordsCount > MAX_COUNT) {
text = "";
for(int i = 0; i < MAX_COUNT; i += 1) {
text += segments[i];
}
text += " Read more...";
}
textView.setText(text);
关于android - 如何在 TextView 中设置最大字长而不是字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25338552/