【问题标题】:Button Text Formats Don't Work On Older API's按钮文本格式不适用于旧 API
【发布时间】:2018-05-21 20:29:26
【问题描述】:

我正在尝试设置一个带有如下文本的按钮:

$34.99 $24.99

但是,在某些较旧的设备上,格式根本不会出现。我试过Html.fromHtml,以及自己构建SpannableString,但都不起作用!

【问题讨论】:

  • Button 的默认样式将textAllCaps 设置为true。在 Oreo 之前,涉及的转换将文本视为纯 String,因此您最终会丢失已应用的任何跨度格式。如果您要将Buttons 的样式设置为textAllCapsfalse,它应该可以解决您的问题,而无需切换到TextView
  • 你是对的!我只是尝试将它们改回按钮,并且有效。谢谢你的解释!

标签: android button formatting textview


【解决方案1】:

无论出于何种原因,将Button 更改为TextView 似乎可以解决问题!

编辑:使用Button 可以正常工作,但您需要将textAllCaps 设置为false 以获取26 之前的API。感谢@Mike M. 的解释!

下面的代码现在可以工作了:

SpannableString spannableString = new SpannableString(oldBuyPrice + newBuyPrice);
spannableString.setSpan(new StyleSpan(Typeface.ITALIC), 0, oldBuyPrice.length(), 0);
spannableString.setSpan(new StrikethroughSpan(), 0, oldBuyPrice.length(), 0);
buyButtonTextView.setText(spannableString);

【讨论】:

    猜你喜欢
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 2014-10-18
    • 1970-01-01
    相关资源
    最近更新 更多