Button setText with Spannable doesn't work for Android 5.0 Lollipop

守給你的承諾、 提交于 2019-11-27 04:45:35

问题


I have a simple 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);

It works on Android 4.3 but doesn't on 5.0.

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.


回答1:


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.

You can override the default button styling and disable all-caps by specifying android:textAllCaps="false" on your Button.

<Button
    ...
    android:textAllCaps="false" />


来源:https://stackoverflow.com/questions/29007746/button-settext-with-spannable-doesnt-work-for-android-5-0-lollipop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!