自定义HtmlTextView,让textView显示多种颜色的文字

转载请注明出处,谢谢http://blog.csdn.net/harryweasley/article/details/71158181

网络上textView显示各种颜色的文字,都是在java代码里实现的,类似下面这种方式:

比如 (< Android Nougat):

myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));

比如(>= Android Nougat):

myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_LEGACY));

上面的两种方式都是通过java代码来实现的,因为某些textView只是显示文字,你根本不需要得到他的TextView对象,那么是否可以直接在xml就实现好,而不需要在java代码里得到TextView对象,再设置呢。

首先看下效果图:
这里写图片描述

自定义一个HtmlTextView,如下所示:

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  on 2017/5/3.
 */

public class HtmlTextView extends AppCompatTextView{

    public HtmlTextView(Context context) {
        super(context);
        setText(fromHtml(getText().toString()));
    }

    public HtmlTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setText(fromHtml(getText().toString()));
    }

    public HtmlTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        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;
    }
}

valuse/strings.xml里的内容如下所示:

<resources>

    <string name="app_name">app</string>
    <string name="screenshots"><![CDATA[ 1.请<font color=#fb3c56>测试</font>本页]]> </string>
    <string name="choose_pic_to_pay"><![CDATA[ 2.测试测试测试,选择右上角&quot;<font color=#fb3c56>+</font>&quot;号,选择<font color=#fb3c56>测试</font>,
    再选择&quot;<font color=#fb3c56>图库</font>&quot;.]]> </string>
    <string name="ensure_pay_success"><![CDATA[ 3.测试后,请点击&quot;<font color=#fb3c56>测试成功</font>&quot;按钮]]> </string>
</resources>

最后直接在activity.xml中放入即可,如下所示:

<?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"
              android: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_marginTop="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>

通过上面的方式,我们不必去得到TextView的对象,在java代码里将其设置,直接在xml里,就可搞定啦。

猜你喜欢

转载自blog.csdn.net/HarryWeasley/article/details/71158181