在做项目的时候,经常会遇到过一行文字有两种颜色。有时候直接会想到用多个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