【问题标题】:How to set the text color of TextView in code?如何在代码中设置TextView的文字颜色?
【发布时间】:2017-02-04 23:57:57
【问题描述】:

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

我尝试了类似的方法:

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

其中holder 只是一个类,textTextView 类型。红色是在字符串中设置的 RGB 值 (#FF0000)。

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

【问题讨论】:

  • 关于在代码中调整 UI 的说明,请考虑在设计时查看 UI 并将运行时更改降至最低的优势。

标签: android colors textview


【解决方案1】:

如果你使用Kotlin,有4种方式:(使用Holder)

  1. 使用Android资源

    holder.textView.setTextColor(Color.GREEN)

  2. 使用RGB

    holder.textView.setTextColor(Color.rgb(255, 87, 34))

3)使用十六进制

holder.textView.setTextColor(Color.parseColor("#C2185B"))

4)使用项目资源:(需要 API 级别 23)

holder.textView.setTextColor(context.resources.getColor(R.color.colorMax,null))

【讨论】:

    【解决方案2】:
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { 
        b.numberDay1.setTextColor(ContextCompat.getColor(requireContext(), R.color.secondary_100))
    } else {                
        b.numberDay1.setTextColor(resources.getColor(R.color.secondary_100))
    }
    

    【讨论】:

      【解决方案3】:
      TextView color= (TextView)findViewById(R.id.color);
      text.setTextColor(Color.RED);
      

      【讨论】:

        【解决方案4】:

        你应该使用:

        holder.text.setTextColor(Color.RED);
        

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

        • Color.parseColor(Manual)(就像 LEX 使用的那样)

          text.setTextColor(Color.parseColor("#FFFFFF"));
          
        • Color.rgbColor.argb (Manual rgb) (Manual argb)(就像 Ganapathy 使用的那样)

          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 使用它,如现在的示例所示。

        【讨论】:

        • 另外,如果文本是链接需要使用text.setLinkTextColor(...);在代码中或 android:textColorLink="..." 在 XML 中
        • @Nanne 如果您的回答还提到 R.color.XXX 是对颜色的引用,那就太好了。这意味着为了清楚起见,它需要被取消引用(就像在你的例子中一样)。
        • 我不确定你的意思?如,取消引用,因此会使用更多资源还是您的意思是别的?
        • getColor(int) 已弃用。
        • 如何导入颜色类?
        【解决方案5】:

        Kotlin 扩展解决方案

        添加这些以使更改文本颜色更简单

        用于设置 ColorInt

        myView.textColor = Color.BLACK // or Color.parseColor("#000000"), etc.
        
        var TextView.textColor: Int
        get() = currentTextColor
        set(@ColorInt color) {
            setTextColor(color)
        }
        

        用于设置 ColorRes

        myView.setTextColorRes(R.color.my_color)
        
        fun TextView.setTextColorRes(@ColorRes colorRes: Int) {
            val color = ContextCompat.getColor(context, colorRes)
            setTextColor(color)
        }
        

        【讨论】:

          【解决方案6】:

          试试这个:

          TextView textview = (TextView) findViewById(R.id.textview );
          textview .setTextColor(Color.parseColor("#85F85F"));
          

          【讨论】:

            【解决方案7】:

            试试这个:

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

            【讨论】:

            • 上面提到过,但这里需要重复一下:getColor 从 android M 开始已被弃用。它可能有一天会消失。
            【解决方案8】:
            holder.text.setTextColor(Color.rgb(200,0,0));
            

            myTextView.setTextColor(0xAARRGGBB);
            

            【讨论】:

              【解决方案9】:
              text1.setTextColor(Color.parseColor("#000000"));
              

              【讨论】:

              【解决方案10】:

              text.setTextColor(getResource().getColor(R.color.black)) 你已经在 color.xml 中创建了黑色。

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

              text.setTextColor(Color.BLACK)你可以使用静态色域

              【讨论】:

                【解决方案11】:
                textView.setTextColor(ContextCompat.getColor(getApplicationC‌​ontext(),R.color.col‌​orWhite)); 
                

                colors.xml文件中,写入以下代码:

                <color name="colorWhite">#FFFFFF</color>
                

                【讨论】:

                  【解决方案12】:

                  从 API 23 开始,getResources().getColor() 已弃用。

                  改用这个:

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

                  【讨论】:

                    【解决方案13】:
                    TextView text = new TextView(context);
                    text.setTextColor(Color.parseColor("any hex value of a color"));
                    

                    以上代码对我有用。这里text 是一个需要设置颜色的TextView

                    【讨论】:

                      【解决方案14】:

                      您可以使用textView.setTextColor(Color.BLACK) 来使用Color 类的任何内置颜色。

                      您还可以使用textView.setTextColor(Color.parseColor(hexRGBvalue)) 定义自定义颜色。

                      【讨论】:

                        【解决方案15】:

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

                        1. 在工作室 res->values->colors.xml 中添加颜色值

                          <color name="color_purple">#800080</color>
                          

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

                          text.setTextColor(getResources().getColor(R.color.color_purple)
                          
                        2. 如果要直接给出颜色代码,请使用下面的 Color.parseColor 代码

                          textView.setTextColor(Color.parseColor("#ffffff"));   
                          
                        3. 你也可以使用RGB

                          text.setTextColor(Color.rgb(200,0,0));
                          
                        4. 使用也可以直接使用 hexcode 进行 textView。您还可以插入普通的 HEX,如下所示:

                          text.setTextColor(0xAARRGGBB);
                          
                        5. 您还可以将 argb 与 alpha 值一起使用。

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

                          a 表示 Alpha(透明)v.

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

                             text.setTextColor(ContextCompat.getColor(context, R.color.color_purple));
                          

                        【讨论】:

                          【解决方案16】:

                          我正在为 RecyclerView 的 ViewHolder 中的 TextView 执行此操作。我不太清楚为什么,但它在 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));
                          }
                          

                          希望这对某人有所帮助。

                          【讨论】:

                            【解决方案17】:

                            getColor() 已弃用

                            所以试试这个方法:

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

                            【讨论】:

                              【解决方案18】:

                              如果您仍想在 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 进行编译,第一个可能更可取,但是您传入的主题可以为空,所以这对您来说可能更容易?

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

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

                              【讨论】:

                              • 对于 setTextColor,为什么它必须采用 getResources().getColor() 而不是直接的 R.color.errorColor 引用? R.color.x 适用于几乎所有其他方法。令人难以置信的令人沮丧!
                              • @Civilian:因为 setXXXColor() 方法所需的 int 参数被视为要使用的实际 ARGB 值,而不是要在资源文件中查找的值。奇怪的是,View 类同时具有 setBackgroundColor() 和 setBackgroundResource(),而 TextView 缺少 setTextResource() 方法。
                              • getColor(int) 已弃用。 ContextCompat.getColor(getContext(), R.color.yourColor); 似乎是替代品。
                              【解决方案19】:
                              holder.userType.setTextColor(context.getResources().getColor(
                                                  R.color.green));
                              

                              【讨论】:

                                【解决方案20】:
                                TextView textresult = (TextView)findViewById(R.id.textView1);
                                textresult.setTextColor(Color.GREEN);
                                

                                【讨论】:

                                  【解决方案21】:

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

                                  holder.text.setTextColor(holder.text.getContext().getResources().getColor(R.color.myRed));
                                  

                                  【讨论】:

                                    【解决方案22】:

                                    要设置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"));
                                    

                                    【讨论】:

                                      【解决方案23】:

                                      尝试使用以下代码:

                                      holder.text.setTextColor(Color.parseColor("F00"));
                                      

                                      【讨论】:

                                        【解决方案24】:

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

                                        【讨论】:

                                          【解决方案25】:

                                          在 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" />
                                          

                                          【讨论】:

                                            【解决方案26】:

                                            我是这样做的: 在 res/values 文件夹中创建一个名为 Colors 的 XML 文件。

                                            我的 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 中使用它,而是从 BaseAdapter 到 ListView。这就是我使用这个上下文对象的原因。

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

                                            希望对你有帮助。

                                            【讨论】:

                                              【解决方案27】:

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

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

                                              或者如果您想从颜色文件夹中提供颜色代码,请使用

                                              textView.setTextColor(R.color.white);
                                              

                                              【讨论】:

                                              • 这段代码 textView.setTextColor(R.color.white);不起作用。您可以使用 text.setTextColor(getResources().getColor(R.color.color_purple) 从您的 color.xml 中获取颜色
                                              【解决方案28】:

                                              同样,我使用的是color.xml

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

                                              用于设置TextView 背景,如:

                                              textView.setTextColor(R.color.white);
                                              

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

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

                                              【讨论】:

                                                【解决方案29】:

                                                用途:

                                                TextView tv = new TextView(this);
                                                tv.setTextColor(Color.rgb(285,0,0));
                                                

                                                【讨论】:

                                                  【解决方案30】:

                                                  我相信,如果您想将颜色指定为资源(在 XML 文件中),则必须提供其 ARGB 值(而不仅仅是 RGB 值)。

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

                                                  【讨论】:

                                                  • 根据我的经验,这不是真的,可以使用 RGB 值而不是 ARGB 值
                                                  猜你喜欢
                                                  • 1970-01-01
                                                  • 2011-04-09
                                                  • 1970-01-01
                                                  • 1970-01-01
                                                  • 1970-01-01
                                                  • 1970-01-01
                                                  • 1970-01-01
                                                  • 2017-01-12
                                                  相关资源
                                                  最近更新 更多