Customize Htmltextview to allow TextView to display multiple colors of text

Source: Internet
Author: User
Tags cdata gettext

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 &quot;<font color= #fb3c56 >+</font>&quot;, select <font color= #fb3c56 > Test </font>,
    then select &quot;<font color= #fb3c56 > Gallery </font>&quot;.]] > </string>
    <string name= "Ensure_pay_success" ><![ cdata[3. After testing, please click &quot;<font color= #fb3c56 > Test Success </font>&quot; 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.