900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Android开发:一个TextView中设置文字不同字体大小和颜色的2种高效方法

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

时间:2020-01-06 03:26:27

相关推荐

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

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

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知识,可查看以下文档:

/jdsjlzx/article/details/19122103

/zuo_er_lyf/article/details/80340819

/lukejunandroid/article/details/25892737

源码下载:

/download/android157/11226092

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。