Android开发:一个TextView中设置文字不同字体大小和颜色的2种高效方法

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

效果:

 

先看一下xml代码:

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Hello World!" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="TextView" />

</LinearLayout>

 

大家可以对应着布局中的id对应着看一下,文字设置:


//上图中的第一个TextView代码,字体颜色,为文字设置颜色

String strMsg = "今天<font color=\"#00ff00\">天气不错</font>";
tv_msg.setText(Html.fromHtml(strMsg));

 

 

//上图中的第二个TextView代码,字体,用Html的方式格式化字体,是不支持用size属性设置字体的大小的,只能使用标签进行格式化。小字体标签可以嵌套,会显示小的字体

String str1 = "今天<font color= \"#00ff00\"><small>天气不错</small></font>";
textView1.setText(Html.fromHtml(str1));

 

 

//上图中的第三个TextView代码,字体,大字体标签可以嵌套,会显示大的字体

String str2 = "今天<font color=\"#00ff00\"><big>天气不错</big></font>";
textView2.setText(Html.fromHtml(str2));


 

//上图中的第四个TextView代码,字体,两个小字体标签可以嵌套,会显示更小的字体

String str3 = "今天<font color = \"#00ff00\"><small><small>天气不错</small></small></font>";
textView3.setText(Html.fromHtml(str3));

 

 

//上图中的第五个TextView代码,字体,两个大字体标签可以嵌套,会显示更大的字体

String str4 = "今天<font color=\"#00ff00\"><big><big>天气不错</big></big></font>";
textView4.setText(Html.fromHtml(str4));

 

 

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

String str5 = "天气不错";
textView5.setText(Html.fromHtml("今天" + "<font color=\"#00ff00\">" + str5 + "</font>"));

 

 

//上图中的第七个TextView代码,一段文字设置两种不同的颜色

String str6 = "<font color=\"#00ff00\">今天</font><font color=\"#0000ff\">天气不错</font>";
textView6.setText(Html.fromHtml(str6));


 

//上图中的第八个TextView代码,一个Text中可以让字体显示成两行,加入<br>标签

String str7 = "<font color=\"#00ff00\">今天</font><br><font color=\"#0000ff\">天气不错</font>";
textView7.setText(Html.fromHtml(str7));

 

 

// 上图中的第九个TextView代码,使用SpannableString实现不同字体

SpannableString spannableString = new SpannableString("今天天气不错");
spannableString.setSpan(new AbsoluteSizeSpan(60), 2, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#ffffff")), 2, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#00ff00")), 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new BackgroundColorSpan(Color.parseColor("#999999")), 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView8.setText(spannableString);

 

了解更多SpannableString知识,可查看以下文档:

https://blog.csdn.net/jdsjlzx/article/details/19122103

https://blog.csdn.net/zuo_er_lyf/article/details/80340819

https://blog.csdn.net/lukejunandroid/article/details/25892737

 

源码下载:

https://download.csdn.net/download/android157/11226092

 

 

  • 15
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您可以通过使用 SpannableString 和 ClickableSpan 类来实现在 TextView 设置不同字体大小颜色和点击事件的效果。 要设置部分文字字体大小颜色,您可以使用 RelativeSizeSpan 和 ForegroundColorSpan。下面是一个示例代码: ```java String text = "这是一个示例文本"; SpannableString spannableString = new SpannableString(text); // 设置部分文字字体大小 RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.5f); // 放大1.5倍 spannableString.setSpan(sizeSpan, 2, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE); // 设置第2到第5个字符的字体大小 // 设置部分文字颜色 ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.RED); spannableString.setSpan(colorSpan, 8, 10, Spanned.SPAN_INCLUSIVE_INCLUSIVE); // 设置第8到第10个字符的颜色 TextView textView = findViewById(R.id.textView); textView.setText(spannableString); ``` 要设置部分文字的点击事件,您可以使用 ClickableSpan。下面是一个示例代码: ```java String text = "点击这里触发事件"; SpannableString spannableString = new SpannableString(text); ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View widget) { // 在这里处理点击事件 Toast.makeText(MainActivity.this, "点击事件触发了", Toast.LENGTH_SHORT).show(); } }; spannableString.setSpan(clickableSpan, 2, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE); // 设置第2到第4个字符的点击事件 TextView textView = findViewById(R.id.textView); textView.setText(spannableString); textView.setMovementMethod(LinkMovementMethod.getInstance()); // 必须设置这个方法才能触发点击事件 ``` 上述代码,我们通过创建 SpannableString 对象,然后使用 setSpan() 方法设置不同的 Span(包括字体大小颜色和点击事件),最后将 SpannableString 对象设置TextView 显示出来。 希望能帮到您!如有更多问题,请随时提问。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值