Reprint please indicate the source, thank http://blog.csdn.net/harryweasley/article/details/71158181
The TextView display of various colors on the network is implemented in Java code, similar to the following:
For example (< Android nougat):
Mytextview.settext (html.fromhtml ("
For example (>= Android nougat):
Mytextview.settext (html.fromhtml ("
The above two methods are implemented through the Java code, because some textview just display text, you do not need to get his TextView object, then whether it can be done directly in XML, and do not need to be in the Java code to get TextView objects, and then set it.
First look at the effect chart:
Customize a Htmltextview, as follows:
Package com.test;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.support.v7.widget.AppCompatTextView;
Import android.text.Html;
Import android.text.Spanned;
Import Android.util.AttributeSet;
/** * Created by Harry in 2017/5/3. */public class Htmltextview extends appcompattextview{public Htmltextview (context) {Super
);
SetText (fromhtml (GetText (). toString ()));
Public Htmltextview (context, AttributeSet attrs) {Super (context, attrs);
SetText (fromhtml (GetText (). toString ())); Htmltextview (context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, Defstyle
ATTR);
SetText (fromhtml (GetText (). toString ()));
@SuppressWarnings ("deprecation") public static spanned fromhtml (String html) {spanned result; if (Android.os.Build.VERSION.SDK_INT >= Android.os.Build.VERSION_CODES. N) {result = Html.fromhtml (HTML, html.from_html_mode_legacy);
else {result = html.fromhtml (Html);
return result;
}
}
The contents of the Valuse/strings.xml are as follows:
<resources>
<string name= "app_name" >app</string>
<string name= "screenshots" >< ! [cdata[1. Please <font color= #fb3c56 > Test </font> page]]> </string>
<string name= "choose_pic_to_ Pay "><! [cdata[2. Test test, select upper right corner "<font color= #fb3c56 >+</font>", select <font color= #fb3c56 > Test </font>,
then select "<font color= #fb3c56 > Gallery </font>".]] > </string>
<string name= "Ensure_pay_success" ><![ cdata[3. After testing, please click "<font color= #fb3c56 > Test Success </font>" button]]> </string>
</ Resources>
Finally put it directly in the Activity.xml, as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:layout_width=" match_parent "Andr oid:layout_height= "Match_parent" > <com.test.htmltextview android:id= "@+id/screenshots" Android
: layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "22DP" android:text= "@string/screenshots"/> <com.test.htmltextview android:id= "@+id/choose_pic_to_pay "Android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_marginto p= "15DP" android:text= "@string/choose_pic_to_pay"/> <com.test.htmltextview android:id
= "@+id/ensure_pay_success" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"
android:layout_margintop= "15DP" android:text= "@string/ensure_pay_success"/> </LinearLayout>
In the way above, we don't have to get the TextView object, set it in the Java code, directly in the XML, we can do it.