How to set the text color of TextView in code?

后端 未结 30 2577
你的背包
你的背包 2020-11-22 07:48

In XML, we can set a text color by the textColor attribute, like android:textColor=\"#FF0000\". But how do I change it by coding?

I tried s

相关标签:
30条回答
  • 2020-11-22 08:22

    text.setTextColor(getResource().getColor(R.color.black)) you have create black color in color.xml.

    OR

    text.setTextColor(Color.parseColor("#000000")) here type desired hexcode

    OR

    text.setTextColor(Color.BLACK) you can use static color fields

    0 讨论(0)
  • 2020-11-22 08:22
       textViewStatus.setTextColor(res.getColor(R.color.green));
    
    0 讨论(0)
  • 2020-11-22 08:22

    I did this way: Create a XML file, called Colors in res/values folder.

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

    To get this colors from the xml file, I've used this code: valor it's a TextView, and ctx it's a Context object. I'm not using it from an Activity, but a BaseAdapter to a ListView. That's why I've used this Context Object.

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

    Hope it helps.

    0 讨论(0)
  • 2020-11-22 08:23

    use the following code in 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" />
    
    0 讨论(0)
  • 2020-11-22 08:23

    If you plan to use setTextAppearance you should know that it will overwrite the text color with the style inherited from the theme. So if you want to use both, set the color afterwards.

    This works:

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

    While this will cause your textcolor to be for instance white(for dark theme) or black(for the light theme):

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

    Contrary to this in XML the order is arbitrary.

    0 讨论(0)
  • 2020-11-22 08:23
    TextView text = new TextView(context);
    text.setTextColor(Color.parseColor("any hex value of a color"));
    

    Above code is working on my side. Here text is a TextView on which color is needed to be set.

    0 讨论(0)
提交回复
热议问题