首页 文章

如何在代码中设置TextView的文本颜色?

提问于
浏览
473

在XML中,我们可以通过 textColor 属性设置文本颜色,如 android:textColor="#FF0000" . 但是如何通过编码来改变它呢?

我试过类似的东西:

holder.text.setTextColor(R.color.Red);

holder 只是一个类,而 text 的类型是 TextView . 红色是以字符串形式设置的RGB值(#FF0000) .

但它显示的是不同的颜色而不是红色 . 我们可以在setTextColor()中传递什么样的参数?在文档中,它说 int ,但它是资源引用值还是其他什么?

30 回答

  • 11

    在layout.xml中使用以下代码

    <TextView  android:id="@+id/textView1"    
    android:layout_width="wrap_content"    
    android:layout_height="wrap_content" 
    android:text="@string/add"
    android:layout_marginTop="16dp"
    android:textAppearance="?
    android:attr/textAppearanceMedium"
    android:textColor="#25383C"
    android:textSize="13sp" />
    
    <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add"
            android:layout_marginTop="16dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#25383C"
            android:textSize="13sp" />
    
  • 1108

    你应该使用:

    holder.text.setTextColor(Color.RED);
    

    为了进行健全性检查,我只是尝试了,因为无论如何我都打开了一个项目,是的,它很漂亮而且很红; D


    您可以使用 Color 类中的各种函数来获得相同的效果 .

    • Color.parseColor (Manual)(与LEX一样)
    text.setTextColor(Color.parseColor("#FFFFFF"));
    
    holder.text.setTextColor(Color.rgb(200,0,0));
    holder.text.setTextColor(Color.argb(0,200,0,0));
    
    • 当然,如果要在 XML 文件中定义颜色,可以执行以下操作:
    <color name="errorColor">#f00</color>
    

    因为 getColor() 函数已弃用1,您需要像这样使用它:

    ContextCompat.getColor(context, R.color.your_color);
    
    • 您也可以插入普通HEX,如下所示:
    myTextView.setTextColor(0xAARRGGBB);
    

    首先是alpha通道,然后是颜色值 .

    查看完整的手册,当然,public class Color extends Object .


    1这段代码也曾在这里:

    textView.setTextColor(getResources().getColor(R.color.errorColor));
    

    现在,在Android M中不推荐使用此方法 . 但是,您可以在contextCompat in the support library中使用它,如现在的示例所示 .

  • 7

    如果您仍想在XML文件中指定颜色:

    <color name="errorColor">#f00</color>
    

    然后使用以下两种方法之一在代码中引用它:

    textView.setTextColor(getResources().getColor(R.color.errorColor, getResources().newTheme()));
    

    要么

    textView.setTextColor(getResources().getColor(R.color.errorColor, null));
    

    如果你正在编译Android M,那么第一个可能更好,但是你传入的主题可以为null,所以也许这对你来说更容易?

    如果你正在使用Compat库,你可以做这样的事情

    textView.setTextColor(ContextCompat.getColor(context, R.color.errorColor));
    
  • 4

    还有一个:

    TextView text = (TextView) findViewById(R.id.text);
    text.setTextColor(Color.parseColor("#FFFFFF"));
    
  • 3

    您也只能从XML文件中执行此操作 .

    在values文件夹中创建 color.xml 文件:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="textbody">#ffcc33</color>
    
    </resources>
    

    然后在任何XML文件中,您可以使用设置文本的颜色,

    android:textColor="@color/textbody"
    

    或者您可以在Java文件中使用此颜色:

    final TextView tvchange12 = (TextView) findViewById(R.id.textView2);
    //Set color for textbody from color.xml file
    tvchange1.setTextColor(getResources().getColor(R.color.textbody));
    
  • 1

    您可以使用

    holder.text.setTextColor(Color.rgb(200,0,0));
    

    您还可以使用透明度指定所需的颜色 .

    holder.text.setTextColor(Color.argb(0,200,0,0));
    

    a for Alpha (透明)值r-red g-green b-blue

  • 1

    同样,我正在使用 color.xml

    <color name="white">#ffffff</color>
        <color name="black">#000000</color>
    

    用于设置 TextView 背景,如:

    textView.setTextColor(R.color.white);
    

    我得到了不同的颜色,但是当我使用下面的代码时,我得到了实际的颜色 .

    textView.setTextColor(Color.parseColor("#ff6363"));
    
  • 25

    在文本视图上设置颜色有许多不同的方法 .

    • 在studio res-> values-> colors.xml中添加颜色值为
    <color name="color_purple">#800080</color>
    

    现在将xml或actvity类中的颜色设置为

    text.setTextColor(getResources().getColor(R.color.color_purple)
    
    • 如果要直接使用Color.parseColor代码,请使用颜色代码
    textView.setTextColor(Color.parseColor("#ffffff"));
    
    • 您也可以使用RGB
    text.setTextColor(Color.rgb(200,0,0));
    
    • 使用也可以使用textView的直接十六进制代码 . 您也可以插入纯HEX,如下所示:
    text.setTextColor(0xAARRGGBB);
    
    • 您还可以将argb与alpha值一起使用 .
    text.setTextColor(Color.argb(0,200,0,0));
    

    a for Alpha(透明)v .

    • 如果你正在使用Compat库,你可以做这样的事情
    text.setTextColor(ContextCompat.getColor(context, R.color.color_purple));
    
  • 4

    我通常会对任何观点执行此操作:

    myTextView.setTextColor(0xAARRGGBB);
    

    哪里

    • AA定义alpha(00表示透明,FF表示不透明)

    • RRGGBB定义了普通的HTML颜色代码(如红色的FF0000) .

  • 0

    如果您打算使用 setTextAppearance ,您应该知道它将使用从主题继承的样式覆盖文本颜色 . 因此,如果要同时使用两者,请设置颜色 afterwards .

    这有效:

    textView.setTextAppearance(context, android.R.style.TextAppearance_Medium);
    textView.setTextColor(Color.RED);
    

    虽然这会导致你的textcolor例如是白色(对于黑暗主题)或黑色(对于浅色主题):

    textView.setTextColor(Color.RED);
    textView.setTextAppearance(context, android.R.style.TextAppearance_Medium);
    

    与XML相反,顺序是任意的 .

  • 0

    我相信如果你想指定一种颜色作为资源(在XML文件中),你必须提供它的ARGB值(不仅仅是RGB值) .

    尝试将颜色值更改为 #FFFF0000 . 它应该给你RED .

  • 1

    使用:

    TextView tv = new TextView(this);
    tv.setTextColor(Color.rgb(285,0,0));
    
  • 1
    textView.setTextColor(ContextCompat.getColor(getApplicationC‌​ontext(),R.color.col‌​orWhite));
    

    colors.xml 文件中,写下面的代码:

    <color name="colorWhite">#FFFFFF</color>
    
  • 11
    holder.text.setTextColor(Color.rgb(200,0,0));
    

    要么

    myTextView.setTextColor(0xAARRGGBB);
    
  • 1

    使用适配器,您可以使用以下代码设置文本颜色:

    holder.text_view = (TextView) convertView.findViewById(R.id.text_view);
    holder.text_view.setTextColor(Color.parseColor("#FF00FF"));
    
  • 0
    TextView text = new TextView(context);
    text.setTextColor(Color.parseColor("any hex value of a color"));
    

    以上代码正在我身边 . 这里 textTextView ,需要设置颜色 .

  • 1
    text1.setTextColor(Color.parseColor("#000000"));
    
  • 3

    text.setTextColor(getResource().getColor(R.color.black)) 您在color.xml中创建了黑色 .

    要么

    text.setTextColor(Color.parseColor("#000000")) 这里输入所需的十六进制代码

    要么

    text.setTextColor(Color.BLACK) 您可以使用静态颜色字段

  • 42
    textViewStatus.setTextColor(res.getColor(R.color.green));
    
  • 35

    在适配器中,您可以使用以下代码设置文本颜色:

    holder.my_text_view = (TextView) convertView.findViewById(R.id.my_text_view);
    holder.my_text_view.setTextColor(Color.parseColor("#FFFFFF"));
    
  • 8

    如果你想直接给出颜色代码然后使用

    textView.setTextColor(Color.parseColor("#ffffff"));
    

    或者如果你想从颜色文件夹中给出颜色代码,那么使用

    textView.setTextColor(R.color.white);
    
  • 5

    为了设置TextView的颜色, TextView.setTextColor(R.color.YOURCOLOR) 是不够的!

    它必须像这样使用 -

    TextView myText = (TextView) findViewById(R.id.YoutTextViewID);
    
    myText.setTextColor(getResources().getColor(R.color.YOURCOLOR);
    

    要么

    myText.setTextColor(Color.parseColor("#54D66A"));
    
  • 4
    holder.userType.setTextColor(context.getResources().getColor(
                        R.color.green));
    
  • 3

    从API 23开始,不推荐使用 getResources().getColor() .

    改为使用它:

    textView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.color_black));
    
  • 0

    我做到了这样:创建一个XML文件,在res / values文件夹中称为Colors .

    我的Colors.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="vermelho_debito">#cc0000</color>
        <color name="azul_credito">#4c4cff</color>
        <color name="preto_bloqueado">#000000</color>
        <color name="verde_claro_fundo_lista">#CFDBC5</color>
        <color name="branco">#ffffff</color>
        <color name="amarelo_corrige">#cccc00</color>
        <color name="verde_confirma">#66b266</color>
    </resources>
    

    为了从xml文件中获取这些颜色,我使用了这段代码:valor它是一个TextView,而ctx它是一个Context对象 . 我没有在Activity中使用它,而是在ListView中使用BaseAdapter . 这就是我使用这个Context对象的原因 .

    valor.setTextColor(ctx.getResources().getColor(R.color.azul_credito));
    

    希望能帮助到你 .

  • 2

    提供rgb值: text.setTextColor(Color.rgb(200,0,0));
    要从十六进制值解析颜色: text.setTextColor(Color.parseColor("#FFFFFF"));

  • 130

    如果您在适配器中并且仍想使用资源中定义的颜色,则可以尝试以下方法:

    holder.text.setTextColor(holder.text.getContext().getResources().getColor(R.color.myRed));
    
  • 0
    TextView textresult = (TextView)findViewById(R.id.textView1);
    textresult.setTextColor(Color.GREEN);
    
  • 0

    getColor()被删除了

    所以尝试这样:

    tv_title.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.sf_white));
    
  • 0

    我在ViewHolder中为RecyclerView执行此操作 . 我不太清楚为什么,但它在ViewHolder初始化中对我不起作用 .

    public ViewHolder(View itemView) {
        super(itemView);
        textView = (TextView) itemView.findViewById(R.id.text_view);
        textView.setTextColor(context.getResources().getColor(R.color.myColor));
        // Other stuff
    }
    

    但当我将它移动到onBindViewHolder时,它运行正常 .

    public void onBindViewHolder(ViewHolder holder, int position){
        // Other stuff
        holder.textView.setTextColor(context.getResources().getColor(R.color.myColor));
    }
    

    希望这有助于某人 .

相关问题