Android TextView:如果 setText 来自 HTML 和 android:singleLine="true",则文本不显示
Android TextView: text not shown if setText from HTML and android:singleLine="true"
我正在将文本设置为调用
的 TextView
tv.setText(Html.fromHtml("<a href=\"" + link + "\">" + text + "</a>"));
本文是网页link。
在我自己剪切文本之前,它工作正常。
现在我已经设置了:
android:singleLine="true"
android:ellipsize="end"
对于 TextView,文本不再可见。
这是已知问题还是什么?
编辑:
为了让其他人更好地理解问题,我详细解释发生了什么:
这是我的 TextView:
<TextView
android:id="@+id/txtEventRecipient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="true"
android:focusable="false"
android:gravity="center"
android:linksClickable="true"
android:padding="5dp"
android:textColor="@drawable/elv_title_1_selector"
android:textSize="@dimen/small_text_size"
android:textStyle="bold"
android:typeface="normal" />
这是我将文本设置为 link:
的代码
TextView tv = (TextView) convertView.findViewById(R.id.txtEventRecipient);
String userName = "<a user name got from somewhere>";
// here I was cutting the userName as it's length must fit
if (userName.length() > 16)
userName = userName.substring(0, 15) + "…";
// here I set the link to the user
tv.setText(Html.fromHtml("<a href=\"" + link + "\">" + userName + "</a>"));
tv.setMovementMethod(LinkMovementMethod.getInstance());
这是可行的,但最大长度固定为可怕的 16
,所以我决定使用 "singleLine".
使其更具动态性
singleLine="true"
导致文本不显示为 HTML link。
android:singleLine
已经或多或少被弃用了一段时间。它很少起作用。 android:maxLines="1"
现在比较典型。
我正在将文本设置为调用
的 TextViewtv.setText(Html.fromHtml("<a href=\"" + link + "\">" + text + "</a>"));
本文是网页link。
在我自己剪切文本之前,它工作正常。 现在我已经设置了:
android:singleLine="true"
android:ellipsize="end"
对于 TextView,文本不再可见。
这是已知问题还是什么?
编辑:
为了让其他人更好地理解问题,我详细解释发生了什么:
这是我的 TextView:
<TextView
android:id="@+id/txtEventRecipient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="true"
android:focusable="false"
android:gravity="center"
android:linksClickable="true"
android:padding="5dp"
android:textColor="@drawable/elv_title_1_selector"
android:textSize="@dimen/small_text_size"
android:textStyle="bold"
android:typeface="normal" />
这是我将文本设置为 link:
的代码TextView tv = (TextView) convertView.findViewById(R.id.txtEventRecipient);
String userName = "<a user name got from somewhere>";
// here I was cutting the userName as it's length must fit
if (userName.length() > 16)
userName = userName.substring(0, 15) + "…";
// here I set the link to the user
tv.setText(Html.fromHtml("<a href=\"" + link + "\">" + userName + "</a>"));
tv.setMovementMethod(LinkMovementMethod.getInstance());
这是可行的,但最大长度固定为可怕的 16
,所以我决定使用 "singleLine".
singleLine="true"
导致文本不显示为 HTML link。
android:singleLine
已经或多或少被弃用了一段时间。它很少起作用。 android:maxLines="1"
现在比较典型。