在远程视图中使用粗体文本

Using bold text in remote view

 //Setting our own layout for notification (instead of the regular android notification)
            RemoteViews contentView = new RemoteViews(packageName, R.layout.new_event_notification);
            contentView.setTextViewText(R.id.eventName,  eventToDisplay.getName());

            DateTime todayDate = new DateTime().now();


            //If the event's date equal to today's date
            if ((Days.daysBetween(eventToDisplay.getEventDate().withTimeAtStartOfDay(),todayDate.withTimeAtStartOfDay()).getDays())==0)
            {
                SpannableString eventDayOfTheWeekTxt= new SpannableString("Today");
                eventDayOfTheWeekTxt.setSpan(new StyleSpan(Typeface.BOLD),0,4,0);
                contentView.setTextViewText(R.id.eventDayOfTheWeekTxt, eventDayOfTheWeekTxt+ "(" + eventToDisplay.getEventDate().dayOfWeek().getAsText() + ")");

            }

我想把 "today" 这个词加粗,我在这个论坛上找到了一些东西,正如你所看到的......但它没有用。

编辑代码:

 //If the event's date equal to today's date
            if ((Days.daysBetween(eventToDisplay.getEventDate().withTimeAtStartOfDay(),todayDate.withTimeAtStartOfDay()).getDays())==0)
            {
                SpannableString eventDayOfTheWeekTxt= new SpannableString("Today");
                contentView.setTextViewText(R.id.eventDayOfTheWeekTxt, eventDayOfTheWeekTxt+ "(" + eventToDisplay.getEventDate().dayOfWeek().getAsText() + ")");
                eventDayOfTheWeekTxt.setSpan(new StyleSpan(Typeface.BOLD), 0, 4, 0);


            }

那是因为您要通过串联删除跨度。使用 TextUtils.concat() 而不是 +,或者在 字符串连接之后应用 BOLD 跨度


在 Java 编程中,串联创建新对象。

替换:

SpannableString eventDayOfTheWeekTxt= new SpannableString("Today");
contentView.setTextViewText(R.id.eventDayOfTheWeekTxt, eventDayOfTheWeekTxt+ "(" + eventToDisplay.getEventDate().dayOfWeek().getAsText() + ")");
eventDayOfTheWeekTxt.setSpan(new StyleSpan(Typeface.BOLD), 0, 4, 0);

与:

SpannableStringBuilder eventDayOfTheWeekTxt=new SpannableStringBuilder("Today");

eventDayOfTheWeekTxt.append("(");
eventDayOfTheWeekTxt.append(eventToDisplay.getEventDate().dayOfWeek().getAsText());
eventDayOfTheWeekTxt.append(")");
eventDayOfTheWeekTxt.setSpan(new StyleSpan(Typeface.BOLD), 0, 4, 0);
contentView.setTextViewText(R.id.eventDayOfTheWeekTxt, eventDayOfTheWeekTxt);