具有Spannable的按钮setText不适用于Android 5.0 Lollipop [英] Button setText with Spannable doesn't work for Android 5.0 Lollipop

查看:94
本文介绍了具有Spannable的按钮setText不适用于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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆