Android安卓TextView部分字体颜色

迈不过友情╰ 2021-10-18 12:50 670阅读 0赞

TextView修改部分文字颜色

  • Java代码
  • 或Kotlin代码
  • 布局文件
  • 其运行效果

Java代码

  1. String colorText = "这里是<font color = \"#FF0000\">红色</font>的,这里是<font color = \"#03A9F4\">蓝色</font>的";
  2. TextView textView = findViewById(R.id.textView);
  3. textView.setText(Html.fromHtml(colorText));

或Kotlin代码

  1. //修改TextView部分文字颜色
  2. val colorText = "这里是<font color = \"#FF0000\">红色</font>的,这里是<font color = \"#03A9F4\">蓝色</font>的"
  3. val textView = findViewById<TextView>(R.id.textView)
  4. textView.text = Html.fromHtml(colorText)

布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
  3. <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" android:textSize="18dp" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
  4. </android.support.constraint.ConstraintLayout>

其运行效果

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,670人围观)

还没有评论,来说两句吧...

相关阅读