问题描述
我有一个简单的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.
推荐答案
默认情况下,Material 按钮的样式设置为以全大写显示文本.但是,用于大写的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!