1

我可以SpannableStrings在小部件的 textView 中使用吗?我试过了,它呈现的只是纯文本。我不知道这是否是我做错了(很可能),或者是否由于某种原因不可能。

这是我正在使用的代码(真的没什么特别的......)

 public static void updateWidgetState(Context paramContext, String paramString, Integer appWidgetId) {

    SpannableString majorLabel = new SpannableString("");
    SpannableString minorLabel = new SpannableString("");
    if (position > -1) {
        majorLabel = GetParsedMajorLabel(paramContext);
        minorLabel = GetParsedMinorLabel(paramContext);
    }
    RemoteViews localRemoteViews = buildUpdate(paramContext, paramString, appWidgetId);
    localRemoteViews.setTextViewText(R.id.majorlabel, majorLabel);
    localRemoteViews.setTextViewText(R.id.minorlabel, minorLabel);
 }
4

4 回答 4

0
String urlink = "http://www.google.com";
String link = "<a href=\"+urlink+ >link</a>"; 
textView.setText(Html.fromHtml(link));
于 2012-06-04T20:23:39.783 回答
0

如果您想在 a 中显示格式化文本TextView,请使用fromHtml

于 2012-06-04T20:15:02.790 回答
0

2020 年更新 Html.fromHtml 在 Android N 中已弃用

用于HtmlCompat.fromHtml更好的兼容性

使用 HTML 设置粗体文本的示例

 setTextViewText(
                  R.id.wind, HtmlCompat.fromHtml(
                            "<b>Wind:</b>"+ windSpeedKmh+"<br/>"+
                            "<b>Direction:</b>" +windDirection
                       , HtmlCompat.FROM_HTML_MODE_LEGACY)
                    )
于 2020-06-15T10:50:49.737 回答
0

您可以轻松地在小部件中设置 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
}
于 2018-11-08T07:53:57.520 回答