方法一
利用HTML语言,改变文字颜色
例1:
String str = "<font color='red'>中软</font>" + "<font color= 'green'>国际</font>";
TextView tv = new TextView(this);
tv.setText(Html.fromHtml(str));
例2:
<string name="link_text_manual"><b>text2:</b> This is some other
text, with a <a href="http://www.google.com">link</a> specified via an <a> tag. Use a \"tel:\" URL
to <a href="tel:4155551212">dial a phone number</a>
</string>
方法二
通过定义不同的style来设置TextView的颜色和大小
例1:
<style name="style0">
<item name="android:textSize">19dip</item>
<item name="android:textColor">@color/color1</item>
</style>
<style name="style1">
<item name="android:textSize">23dip</item>
<item name="android:textColor">@color/color2</item>
<item name="android:textStyle">italic</item>
</style>
SpannableString styledText = new SpannableString("大家好才是真的好");
styledText.setSpan(new TextAppearanceSpan(this, R.style.style0), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
styledText.setSpan(new TextAppearanceSpan(this, R.style.style1), 3, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(styledText, TextView.BufferType.SPANNABLE);
例1:
自动识别链接
在TextView中添加如下属性