java - 如何在 Android 中为 TextView 动态设置颜色

标签 java android textview

在我的应用程序中,我想在 TextView 中显示一些文本,并且我从 server 获取此 text
我应该在 XML 布局中只使用一个 textView,并且我应该为这个 Texts 设置 color
我的 XML:

<TextView
    android:id="@+id/rowExplore_userName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/size3"
    android:layout_marginTop="@dimen/padding8"
    android:layout_toRightOf="@+id/rowExplore_imageCard"
    android:fontFamily="sans-serif"
    android:gravity="left|center_vertical"
    android:text="Adam"
    android:textColor="@color/colorPrimary"
    android:textSize="@dimen/font14" />

JSON:

  "name": "Adam Sandel",
  "info": "liked",
  "movieName": "SpiderMan",

Java 代码:

rowExplore_userName.setText(response.getName() + response.getInfo() + response.getMovieName() );

我想动态设置颜色,如下图所示: enter image description here

我怎么可以呢?请帮助我

最佳答案

你可以使用 HTML

String text = "<font color=#cc0029>First Color</font> <font color=#ffcc00>Second Color</font>";
yourtextview.setText(Html.fromHtml(text));

关于java - 如何在 Android 中为 TextView 动态设置颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46565158/

相关文章:

java - 仅在一行 String Android 中更改文本颜色

java - 需要帮助尝试在 TextView 中制作 gif 动画

JavaFx 标题栏图像始终显示为黑色

java - 在 Amazon SQS 上获取 "peer not authenticated"异常

java - 依赖 "test"范围是否适用于 Maven 中的 "integration-test"阶段?

android - token android.os.BinderProxy@e4f4f2b 无效;你的 Activity 在运行吗?

android - 使用 Apportable 向 Android Manifest 添加权限

java - 结构内部的 JNA 结构

android - 在 RecyclerView 项目上添加多个 onClickListener

Cocoa:在应用程序启动时隐藏 TextView 的自定义滚动条