Use SpannableString in Android Home Screen widgets?

試著忘記壹切 提交于 2019-12-19 11:32:46

问题


Can I use SpannableStrings in a widget's textView? I tried and all it rendered was plain text. I don't know if it is something I'm doing wrong (most likely), or if it just isn't possible for some reason.

Here's the code I'm using (nothing special really...)

 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);
 }

回答1:


If you would like to display formatted text in a TextView, use fromHtml




回答2:


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



回答3:


you can easily set Spannable String in widget. what I did is I used SpannableStringBuilder as set it to 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
}


来源:https://stackoverflow.com/questions/10887574/use-spannablestring-in-android-home-screen-widgets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!