在做项目的时候,经常会遇到过一行文字有两种颜色。有时候直接会想到用多个TextView来实现。今天就介绍一下更为简单的方法,用一个TextView实现。
效果:
先看一下xml代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid:id="@+id/tv_msg"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="Hello World!" /><TextViewandroid:id="@+id/textView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="TextView" /><TextViewandroid:id="@+id/textView2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="TextView" /><TextViewandroid:id="@+id/textView3"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="TextView" /><TextViewandroid:id="@+id/textView4"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="TextView" /><TextViewandroid:id="@+id/textView5"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="TextView" /><TextViewandroid:id="@+id/textView6"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="TextView" /><TextViewandroid:id="@+id/textView7"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:gravity="center"android:text="TextView" /><TextViewandroid:id="@+id/textView8"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="TextView" /><TextViewandroid:id="@+id/textView9"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="TextView" /></LinearLayout>
大家可以对应着布局中的id对应着看一下,文字设置:
//
上图中的第一个TextView代码,字体颜色,为文字设置颜色
tv_msg.setText(Html.fromHtml(strMsg));String strMsg = "今天<font color=\"#00ff00\">天气不错</font>";
//
上图中的第二个TextView代码,字体,用Html的方式格式化字体,是不支持用size属性设置字体的大小的,只能使用标签进行格式化。小字体标签可以嵌套,会显示小的字体
textView1.setText(Html.fromHtml(str1));String str1 = "今天<font color= \"#00ff00\"><small>天气不错</small></font>";
//
上图中的第三个TextView代码,字体,大字体标签可以嵌套,会显示大的字体
textView2.setText(Html.fromHtml(str2));String str2 = "今天<font color=\"#00ff00\"><big>天气不错</big></font>";
//
上图中的第四个TextView代码,字体,两个小字体标签可以嵌套,会显示更小的字体
textView3.setText(Html.fromHtml(str3));String str3 = "今天<font color = \"#00ff00\"><small><small>天气不错</small></small></font>";
//
上图中的第五个TextView代码,字体,两个大字体标签可以嵌套,会显示更大的字体
textView4.setText(Html.fromHtml(str4));String str4 = "今天<font color=\"#00ff00\"><big><big>天气不错</big></big></font>";
//
上图中的第六个TextView代码,上边的情况都是固定字符的情况,那如果遇到变量该怎么办呢?其实也很简单。遇到变量的情况
textView5.setText(Html.fromHtml("今天" + "<font color=\"#00ff00\">" + str5 + "</font>"));String str5 = "天气不错";
//
上图中的第七个TextView代码,一段文字设置两种不同的颜色
textView6.setText(Html.fromHtml(str6));String str6 = "<font color=\"#00ff00\">今天</font><font color=\"#0000ff\">天气不错</font>";
//
上图中的第八个TextView代码,一个Text中可以让字体显示成两行,加入<br>标签
textView7.setText(Html.fromHtml(str7));String str7 = "<font color=\"#00ff00\">今天</font><br><font color=\"#0000ff\">天气不错</font>";
//
上图中的第九个TextView代码,使用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 spannableString = new SpannableString("今天天气不错");
了解更多SpannableString知识,可查看以下文档:
/jdsjlzx/article/details/19122103
/zuo_er_lyf/article/details/80340819
/lukejunandroid/article/details/25892737
源码下载:
/download/android157/11226092