Is it possible to set the color of a string directly in string.xml?

前端 未结 12 1045
轮回少年
轮回少年 2021-02-07 12:17

I mean something like:

Error!
相关标签:
12条回答
  • 2021-02-07 12:28

    Put this code in the string.xml file:

    <font color="Red"><a href="support@legacystories.org">HERE</a></font>
    
    0 讨论(0)
  • 2021-02-07 12:28

    No this is not possible you have to specify that in your layout. But you can put the color in your colors.xml.

    colors.xml

    <color name="foo">#abc123</color>
    

    your_layout.xml

    <TextView android:textColor="@color/foo" android:text="@string/error" />
    
    0 讨论(0)
  • 2021-02-07 12:34

    Strings.xml

    <string name="pbs_setup_title">Select %1$s <font fgcolor="#FF33B5E5">brand</font> or scan the <font fgcolor="#FFFF0000">Remote</font></string>
    

    class.java

    String SelectedDeviceType_Str = "TV"
    
    SetupYourDevice_TextView.setText(Html.fromHtml(String.format(Html.toHtml(new SpannedString(getResources().getText(R.string.pbs_setup_title))),
                        SelectedDeviceType_Str)));
    
    0 讨论(0)
  • 2021-02-07 12:35

    As suggested by rekire not possible to set color the way you are doing.

    You can use the method as suggested by rekire.

    In you xml you can specify color for your textview as

      android:textColor="#0EFFFF"
    

    You can also set text color programaticaly

      TextView tv= (TextView)findviewById(R.id.textView1);
      tv.setTextColor(Color.RED);  
    

    To set color of particular words in textview, you can use spannable string

        TextView tv= (TextView)findviewById(R.id.textView1);
        tv.setText("");  
        String s="Hello World";
        SpannableString ss=  new SpannableString(s);                
        ss.setSpan(new ForegroundColorSpan(Color.GREEN), 0, 5, 0);  
        tv.setText(ss);
    
    0 讨论(0)
  • 2021-02-07 12:40

    Try this one

    <string name="some_text">Font color is <font fgcolor="#ffff0000">red</font></string>
    
    0 讨论(0)
  • 2021-02-07 12:43

    In Strings.xml:

    <resources>
            <color name="etDisabled">#ac8888</color>
            <color name="myorange">#f56415</color>
            <color name="mygreen">#95cd08</color>
      </resources>
    

    Then in the code:

     TextView myText = (TextView) findViewById(R.id.tvJugador1);
      myText.setTextColor(R.color.mygreen);
    
    0 讨论(0)
提交回复
热议问题