前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android项目实战(十四):TextView显示html样式的文字

Android项目实战(十四):TextView显示html样式的文字

作者头像
听着music睡
发布2018-05-18 15:03:26
2.5K0
发布2018-05-18 15:03:26
举报
文章被收录于专栏:Android干货Android干货

项目需求:

TextView显示一段文字,格式为:白雪公主(姓名,字数不确定)向您发来了2(消息个数,不确定)条消息

这段文字中名字和数字的长度是不确定的,还要求名字和数字各自有各自的颜色。

一开始我想的是用(转) SpannableString与SpannableStringBuilder来实现,因为它可以实现一段文字显示不同的颜色

但是貌似它只能固定哪些位置的文字显示什么样式,于是乎放弃。

然后就想到了用 

代码语言:javascript
复制
Html.fromHtml(String str)

来实现。

看方法名很简单,就是可以显示字符串str对应的html格式的文本

比如:

代码语言:javascript
复制
Html.fromHtml(<font color='red' size='24'>你好</font>" )

就将你好以html格式显示了,红色字体 大小24 

那么通过一个小Demo看下这个方法的简单使用:

我有三个字符串,字符串中姓名、数字长度都是不同的,实现让姓名显示红色,数字显示蓝色,其他文字显示默认灰色的效果

先写布局文件,三个TextView

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/html_text"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/html_text2"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/html_text3"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

然后Activity 的onCreate()方法

代码语言:javascript
复制
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.html_text);
        textView2 = (TextView) findViewById(R.id.html_text2);
        textView3 = (TextView) findViewById(R.id.html_text3);
        names = new ArrayList<>();
        counts = new ArrayList<>();
        message = new ArrayList<>();

        names.add("奥特曼");
        names.add("白雪公主与七个小矮人");
        names.add("沃德天·沃纳陌帅·帅德·布耀布耀德 ");

        counts.add(1);
        counts.add(123);
        counts.add(9090909);

        for (int i = 0; i < 3; i++) {
            message.add("<font color='red' size='20'>"+names.get(i)+"</font>"+"向您发来"+
                        "<font color='blue' size='30'>"+counts.get(i)+"</font>"+"条信息");
        }

        textView.setText(Html.fromHtml(message.get(0)));
        textView2.setText(Html.fromHtml(message.get(1)));
        textView3.setText(Html.fromHtml(message.get(2)));

    }

看下效果图,是不是很简单,只要简单的会html 就可实现这种效果

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-12-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档