//字符串替换方法:
TextView textView3 = (TextView) findViewById(R.id.text3);
String text3 = String.format(getResources.getString(R.string.wether), 18, 30, "雷雨大风");
textView3.setText(text3);
//将颜色替换与字符串替换两者结合
TextView textView4 = (TextView) findViewById(R.id.text4);
String text4 = String.format(getResources.getString(R.string.wether), 18, 30, "雷雨大风");
int index[] = new int[3];
index[0] = text4.indexOf("18");
index[1] = text4.indexOf("30");
index[2] = text4.indexOf("雷雨大风");
SpannableStringBuilder style2 = new SpannableStringBuilder(text4);
style2.setSpan(new ForegroundColorSpan(Color.RED), index[0], index[0] + 2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
style2.setSpan(new ForegroundColorSpan(Color.GREEN), index[1], index[1] + 2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
style2.setSpan(new BackgroundColorSpan(Color.YELLOW), index[2], index[2] + 3,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
textView4.setText(style2);
}
}
XML文件
http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:orientation="vertical" >
android:id="@+id/text1"
android:layout_width="wrap_content"