设置范围应用粗体的问题

时间:2017-03-24 18:16:31

标签: android text fonts

我正在尝试设置一个字符串,以便为FAQ部分提供交替的粗体而不是粗体,我看到一些奇怪的东西。只有一组问题和答案会收到正确的范围。调试时,我发现字符串生成器上只设置了两个跨度。它不应该为每个设定范围呼叫附加新的范围吗?

  (activity as MainActivity).settingsPresenter.getFaq(
                    { success, message,value ->
                        if(success) {
                            var sb : SpannableStringBuilder = SpannableStringBuilder("")
                            var fcs : ForegroundColorSpan =  ForegroundColorSpan(Color.rgb(255, 255, 255))
                            var styleBold : StyleSpan = StyleSpan(Typeface.BOLD)
                            var styleNotBold : StyleSpan = StyleSpan(Typeface.NORMAL)

                            for (i in 0..value?.size!! - 1){
                                sb.append(value?.get(i)?.Question + "\n\n")
                                sb.setSpan(styleBold, startLocation, startLocation + value?.get(i)?.Question?.length!!, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                                startLocation += value?.get(i)?.Question?.length!! + 2//update the start location pointer to color the next section of text

                                sb.append(value?.get(i)?.Answer + "\n\n")
                                sb.setSpan(styleNotBold, startLocation, startLocation + value?.get(i)?.Answer?.length!!, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                                startLocation += value?.get(i)?.Answer?.length!! + 2
                            }

                            faq_content.append(sb)

                        }
                    })

1 个答案:

答案 0 :(得分:0)

我能够用这个解决方案来解决这个问题:

 for (i in 0..value?.size!! - 1) {
                            var sb : SpannableStringBuilder = SpannableStringBuilder()
                            sb.append(value?.get(i)?.Question)
                            sb.append("\n\n")
                            sb.setSpan(StyleSpan(android.graphics.Typeface.BOLD), 0, sb.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                            sb.append(value?.get(i)?.Answer)
                            sb.append("\n\n")
                            faq_content.append(sb)
                        }

干杯!