簡體   English   中英

如何在代碼中設置TextView的文本顏色?

[英]How to set the text color of TextView in code?

在 XML 中,我們可以通過textColor屬性設置文本顏色,例如android:textColor="#FF0000" 但是我如何通過編碼來改變它?

我試過類似的東西:

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

其中holder只是一個類,而textTextView類型。 紅色是在字符串中設置的 RGB 值 (#FF0000)。

但它顯示不同的顏色而不是紅色。 我們可以在 setTextColor() 中傳遞什么樣的參數? 在文檔中,它說int ,但它是資源參考值還是其他什么?

你應該使用:

holder.text.setTextColor(Color.RED);

當然,您可以使用Color類中的各種函數來獲得相同的效果。

  • Color.parseColor (手動) (像 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);
  • 您還可以插入普通的十六進制,如下所示:

     myTextView.setTextColor(0xAARRGGBB);

    首先是 alpha 通道,然后是顏色值。

當然,請查看完整的手冊, 公共類 Color extends Object


1此代碼曾經也在這里:

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

此方法現已在 Android M 中棄用。不過,您可以從支持庫中contextCompat 中使用它,如示例所示。

如果您仍想在 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));

還有一個:

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

您也只能從 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));

您可以使用

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

您還可以使用透明度指定所需的顏色。

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

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

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

有許多不同的方法可以在文本視圖上設置顏色。

  1. 在 studio res->values->colors.xml 中添加顏色值作為

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

    現在將 xml 或活動類中的顏色設置為

    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. 使用也可以直接對 textView 使用 hexcode。 您還可以插入普通的十六進制,如下所示:

     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));

我通常對任何視圖都這樣做:

myTextView.setTextColor(0xAARRGGBB);

在哪里

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

  • RRGGBB 定義了正常的 HTML 顏色代碼(如紅色的 FF0000)。

textView.setTextColor(ContextCompat.getColor(getApplicationC‌​ontext(),R.color.col‌​orWhite)); 

colors.xml文件中,寫入以下代碼:

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

如果您打算使用setTextAppearance,您應該知道它將使用從主題繼承的樣式覆蓋文本顏色。 因此,如果您想同時使用兩者,請稍后設置顏色。

這有效:

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

雖然這會導致您的文本顏色例如為白色(對於深色主題)或黑色(對於淺色主題):

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

與此相反,在 XML 中,順序是任意的。

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

或者

text.setTextColor(Color.parseColor("#000000"))在這里輸入所需的十六進制代碼

或者

text.setTextColor(Color.BLACK)您可以使用靜態顏色字段

我相信,如果您想將顏色指定為資源(在XML文件中),則必須提供其ARGB值(而不僅僅是 RGB 值)。

嘗試將您的顏色值更改為#FFFF0000 它應該給你紅色。

用:

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

或者

myTextView.setTextColor(0xAARRGGBB);

使用 Adapter 您可以使用以下代碼設置文本顏色:

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

上面的代碼在我這邊工作。 這里的text是一個需要設置顏色的TextView

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)
}

從 API 23 開始,不推薦使用getResources().getColor()

改用這個:

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

在 Adapter 中,您可以使用以下代碼設置文本顏色:

holder.my_text_view = (TextView) convertView.findViewById(R.id.my_text_view);
holder.my_text_view.setTextColor(Color.parseColor("#FFFFFF"));
   textViewStatus.setTextColor(res.getColor(R.color.green));

如果你想直接給出顏色代碼,那么使用

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

或者,如果您想從顏色文件夾中提供顏色代碼,請使用

textView.setTextColor(R.color.white);

為了設置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"));
holder.userType.setTextColor(context.getResources().getColor(
                    R.color.green));

嘗試這個:

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

同樣,我使用的是color.xml

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

用於設置TextView背景,如:

textView.setTextColor(R.color.white);

我得到了不同的顏色,但是當我使用下面的代碼時,我得到了實際的顏色。

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

用於提供 rgb 值: text.setTextColor(Color.rgb(200,0,0));
從十六進制值解析顏色: text.setTextColor(Color.parseColor("#FFFFFF"));

您可以使用textView.setTextColor(Color.BLACK)來使用Color類的任何內置顏色。

您還可以使用textView.setTextColor(Color.parseColor(hexRGBvalue))來定義自定義顏色。

我是這樣做的:在 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));

希望能幫助到你。

如果您在適配器中並且仍想使用資源中定義的顏色,您可以嘗試以下方法:

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

不推薦使用getColor()

因此,嘗試這種方式:

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

我正在為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));
}

希望這對某人有幫助。

嘗試這個:

textView.setTextColor(getResources().getColor(R.color.errorColor, null));
TextView color= (TextView)findViewById(R.id.color);
text.setTextColor(Color.RED);
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))
}

如果您使用Kotlin ,則有 4 種方式:(使用 Holder)

  1. 使用安卓資源

    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))

嘗試使用以下代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM