您可以轻松地在小部件中设置 Spannable String。我所做的是我将SpannableStringBuilder
其设置为 remoteView's textView
。
views.setTextViewText(R.id.rsTV, WidgetUtils.setPriceSpannableHomeScreenString(currentUserBalance.balance, context))
fun setPriceSpannableHomeScreenString(price: String, context: Context?): SpannableStringBuilder {
val builder = SpannableStringBuilder()
if (AppClass.hasValue(price) && context != null) {
val st = StringTokenizer(price.trim { it <= ' ' }, ".") //pass dot as delimeter
val balanceArrayList = ArrayList<String>()
//iterate through tokens
while (st.hasMoreTokens()) {
balanceArrayList.add(st.nextToken("."))
}
val priceRsText = context.getString(R.string.rs)
var priceBeforeDecimal = ""
var priceAfterDecimal = ""
if (balanceArrayList.size > 0) {
if (balanceArrayList.size > 0 && balanceArrayList.get(0) != null) {
priceBeforeDecimal = balanceArrayList.get(0)
}
if (balanceArrayList.size >= 2 && balanceArrayList.get(1) != null) {
priceAfterDecimal = balanceArrayList.get(1)
}
}
val firstValue = "$priceRsText.$priceBeforeDecimal."
val lastValue = priceAfterDecimal
val txtSpannable = SpannableString(firstValue)
val boldSpan = StyleSpan(Typeface.BOLD)
txtSpannable.setSpan(RelativeSizeSpan(1.5f), 0, firstValue.length, 0) // set size
txtSpannable.setSpan(boldSpan, 0, firstValue.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
builder.append(txtSpannable)
builder.append(lastValue)
// priceSpannableTextView.setText(builder, TextView.BufferType.SPANNABLE)
return builder
}
return builder
}