ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2022-10-10 15:18:20  阅读:234  来源: 互联网

标签:


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

标签:
来源:

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有