具有Spannable的按钮setText不适用于Android 5.0 Lollipop [英] Button setText with Spannable doesn't work for Android 5.0 Lollipop
问题描述
我有一个简单的Button
:
<Button
android:id="@+id/test"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
并尝试通过以下方式更改文本属性:
and try to change text property by:
SpannableString span = new SpannableString(text);
span.setSpan(new AbsoluteSizeSpan(8, true), 5, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
testButton.setText(span);
它可以在Android 4.3上运行,但不能在5.0上运行.
It works on Android 4.3 but doesn't on 5.0.
有趣的是,当我将实现从Button
更改为TextView
时,它在5.0上运行良好.似乎与Lollipop中的Button
有关.
The interesting thing is when I change implementation from Button
to TextView
it works fine on 5.0. Seems to be something with Button
in Lollipop.
推荐答案
默认情况下,材质"按钮的样式设置为大写显示文本.但是,AllCapsTransformationMethod
中存在一个用于大写字母的错误,导致它丢弃了Spannable
数据.
By default, Material buttons are styled to show text in all-caps. However, there is a bug in the AllCapsTransformationMethod
used for capitalization that causes it to discard Spannable
data.
通过在Button
上指定android:textAllCaps="false"
,您可以覆盖默认的按钮样式并禁用全部大写.
You can override the default button styling and disable all-caps by specifying android:textAllCaps="false"
on your Button
.
<Button
...
android:textAllCaps="false" />
这篇关于具有Spannable的按钮setText不适用于Android 5.0 Lollipop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!