有人知道以下代码为什么不更改数据吗?添加了“过期”字符串,但字体颜色和大小均未更改。
Spannable expired = new SpannableString(" expired");
expired.setSpan(new ForegroundColorSpan(Color.RED), 0, expired.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
expired.setSpan(new RelativeSizeSpan(.5f), 0, expired.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
nameTextView.setText(goalsName + expired);
xml:
<TextView
android:id="@+id/goal_list_name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="22sp"
android:layout_marginBottom="8sp"
android:focusable="true"
tools:text="Cel 1" />
答案 0 :(得分:0)
您已使用Spannable
,而应该使用SpannableString
。如下更改代码。
String s = "oldString"+" expired";
SpannableString expired= new SpannableString(s);
expired.setSpan(new ForegroundColorSpan(Color.RED), 10, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
expired.setSpan(new RelativeSizeSpan(.5f), 10, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
nameTextView.setText(expired);
答案 1 :(得分:0)
正确的解决方案:
String s = name + " expired";
SpannableString expired = new SpannableString(s);
expired.setSpan(new ForegroundColorSpan(Color.RED), name.length(), expired.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
expired.setSpan(new RelativeSizeSpan(.7f), name.length(), expired.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
nameTextView.setText(expired);