public class StringUtil { /** * 把str填充到上面的sourceID中的%1$s * @param context * @param textView * @param sourceID String.xml里面的字符串,eg:name="weather_type"对应"天气状况:%1$s" * 就是这里的weather_type * @param str 需要填充的字符串 */ public static void setFormatText(Context context,TextView textView,int sourceID,String str){ String tipFormat = context.getResources().getString(sourceID); textView.setText(String.format(tipFormat, str)); } /** * 使关键字高亮 * @param context * @param textView * @param complete 完整String串 * @param keyword 完整串中需要高亮的关键字 */ public static void highNightKeyword(Context context ,TextView textView,String complete,String keyword){ int index =complete.indexOf(keyword); SpannableStringBuilder builder = new SpannableStringBuilder(complete); ForegroundColorSpan highLightSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.navi_search_hightlight)); builder.setSpan(highLightSpan, index, index + keyword.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(builder); } }