Android 一个TextView中设置文字不同字体大小和颜色的最完整方法

在做项目的时候,经常会遇到过一行文字有两种颜色。有时候直接会想到用多个TextView来实现。今天就介绍一下更为简单的方法,用一个TextView实现。

效果:

这里写图片描述

这几个都实现了一行字有不同颜色,还有两个字体大小也不一样,我们下边一一介绍。

首先xml代码:

<TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textSize="18sp"
        android:text="Hello World!"
        />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textSize="18sp"
        android:text="Hello World!"
        />
    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textSize="18sp"
        android:text="Hello World!"
        />
    <TextView
        android:id="@+id/tv4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textSize="18sp"
        android:text="Hello World!"
        />
    <TextView
        android:id="@+id/tv5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="18sp"
        android:text="Hello World!"
        />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

上图中的第一个TextView代码:

    String str1 = "今天<font color='#FF0000'>天气不错</font>";
    tv1.setText(Html.fromHtml(str1));
  • 1
  • 2

第二个TextView将红色字体变小:

    String str2 = "今天<font color='#FF0000'><small>天气不错</small></font>";
    tv2.setText(Html.fromHtml(str2));
  • 1
  • 2

第三个TextView将红色字体变大:

    String str3 = "今天<font color='#FF0000'><big>天气不错</big></font>";
    tv3.setText(Html.fromHtml(str3));
  • 1
  • 2

上边三种情况都是固定字符的情况,那如果遇到变量该怎么办呢?其实也很简单。第四个TextView代码:

    String str4 = "天气不错";
    tv4.setText(Html.fromHtml("今天" + "<font color='#FF0000'>" + str4 + "</font>"));
  • 1
  • 2

上面的实现方式都是使用的html标签的形式,下面我们使用系统自带的SpannableString来实现。
第五个TextView代码:

    SpannableString spannableString = new SpannableString("今天天气不错");
    spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), 2, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv5.setText(spannableString);
  • 1
  • 2
  • 3

setSpan方法有四个参数,ForegroundColorSpan是为文本设置前景色,也就是文字颜色。如果要为文字添加背景颜色,可替换为BackgroundColorSpan。第二个参数:2为文本颜色改变的起始位置,spannableString.length()为文本颜色改变的结束位置。最后一个参数为布尔型,可以传入以下四种:
Spanned.SPAN_INCLUSIVE_EXCLUSIVE 从起始下标到终止下标,包括起始下标
Spanned.SPAN_INCLUSIVE_INCLUSIVE 从起始下标到终止下标,同时包括起始下标和终止下标
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE 从起始下标到终止下标,但都不包括起始下标和终止下标
Spanned.SPAN_EXCLUSIVE_INCLUSIVE 从起始下标到终止下标,包括终止下标

  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值