在Android主屏幕小部件中使用SpannableString?

时间:2012-06-04 20:04:26

标签: android widget spannablestring

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

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

 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 个答案:

答案 0 :(得分:0)

如果您想在TextView中显示带格式的文字,请使用fromHtml

答案 1 :(得分:0)

String urlink = "http://www.google.com";
String link = "<a href=\"+urlink+ >link</a>"; 
textView.setText(Html.fromHtml(link));

答案 2 :(得分:0)

您可以在小部件中轻松设置Spannable String。我所做的是将SpannableStringBuilder设置为remoteView的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
}

答案 3 :(得分:0)

Android N中不推荐使用Update 2020 Html.fromHtml

使用HtmlCompat.fromHtml以获得更好的兼容性

示例设置带有HTML的粗体

 setTextViewText(
                  R.id.wind, HtmlCompat.fromHtml(
                            "<b>Wind:</b>"+ windSpeedKmh+"<br/>"+
                            "<b>Direction:</b>" +windDirection
                       , HtmlCompat.FROM_HTML_MODE_LEGACY)
                    )