【问题标题】:Is it possible to set the color of a string directly in string.xml?是否可以直接在 string.xml 中设置字符串的颜色?
【发布时间】:2013-04-06 12:07:36
【问题描述】:

我的意思是这样的:

<string name="error" color="#9a1d1d">Error!</string>

【问题讨论】:

  • 也许这个链接可以帮助你:stackoverflow.com/questions/6674183/…
  • 另一种选择是这样的:&lt;string name="error"&gt;&lt;font color="#9a1d1d"&gt;Error!&lt;/font&gt;&lt;/string&gt; 并调用Html.fromHtml(...)。有关示例,请参阅here。就个人而言,我更喜欢 Spannable(Raghunandan 的回答)的灵活性,但有些人认为使用 html 更容易。
  • 这是您的解决方案:stackoverflow.com/questions/15997186/…

标签: android string colors


【解决方案1】:

根据 rekire 的建议,无法按照您的方式设置颜色。

您可以使用rekire建议的方法。

在你的xml中你可以为你的textview指定颜色

  android:textColor="#0EFFFF"

您还可以通过编程方式设置文本颜色

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

要在 textview 中设置特定单词的颜色,可以使用 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);

【讨论】:

  • Thx,我想我会使用这种方法,因为我必须根据情况在同一个textview中打印更多的字符串,所以我无法在布局中设置颜色。
  • 感谢您的代码。它将完全符合我在我的应用程序中的期望。为了快速理解 - 第一个 0 是彩色字符串的起始索引号,5 是彩色字符串的结束字符。第三个参数没有任何用处。谢谢@Raghunandan
  • @sudharsanchandrasekaran 是的,你是对的。但是第三个参数是一个标志。可跨越的独占或包容developer.android.com/reference/android/text/…, int, int, int)。进一步阅读stackoverflow.com/questions/9879233/…
  • 是的!感谢您提供更多新信息@Raghunandan
  • 是否可以将特定文本加粗而不是改变颜色? @Raghunandan
【解决方案2】:

在 4.x 之前,您可以这样做:

<string name="error"><font fgcolor="#ff9a1d1d">Error!</font></string>

但是,错误https://code.google.com/p/android/issues/detail?id=58192 破坏了此功能,因为它引入了一个整数解析器,无法处理设置了最高位的数字,不幸的是,您不能省略颜色的不透明度部分(大多数人更愿意在本例中设置为 ff。)

我昨天刚刚学到了一个聪明的解决方法。你所做的是否定二进制补码中的十六进制颜色值。你如何做到这一点取决于你的十六进制计算器,但最简单的方法是从 0x100000000 中减去你的颜色。在您的情况下,这将导致 0x100000000 - 0xff9a1d1d = 0x65e2e3。 (或者您可以将其反转,例如 0065e2e2 足够接近)。然后你用减号再次否定它:

<string name="error"><font fgcolor="-#65e2e3">Error!</font></string>

瞧!你有你想要的颜色。

感谢 TWiStErRob 在 https://stackoverflow.com/a/11577658/338479 中解决这个问题

ETA:我刚刚发现如果您在 2.x 系统上执行此操作会导致您的应用崩溃;它抛出一个 NumberFormat 异常

【讨论】:

  • 这种方式也不行:String replaceWith = "" + searchString + "";我想要java文件中的代码所以请帮助
【解决方案3】:

试试这个

<string name="some_text">Font color is <font fgcolor="#ffff0000">red</font></string>

【讨论】:

  • 对我没有用,还有其他人可以解决这个问题吗?编辑:啊,等等..你需要为此使用 html.fromhtml
  • 它在 4.x 中中断。请参阅 stackoverflow.com/a/11577658/338479 中 TWiStErRob 的解释和解决方法
  • 状态*
  • 请注意,任何使用这种方法的人都应该使用Resources.getText(id:) 方法来获取字符串,而不是Resources.getString(id:) 方法。前一种方法保留字符串中的任何富文本样式,而后一种方法则不保留。此外,使用Resources.getText(id:) 方法,您可以在font 元素中使用color 属性而不是fgcolor
【解决方案4】:

将此代码放入string.xml文件中:

<font color="Red"><a href="support@legacystories.org">HERE</a></font>

【讨论】:

    【解决方案5】:

    不,这是不可能的,您必须在布局中指定。但是你可以把颜色放在你的colors.xml中。

    colors.xml

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

    your_layout.xml

    <TextView android:textColor="@color/foo" android:text="@string/error" />
    

    【讨论】:

      【解决方案6】:

      字符串.xml

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

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

      【讨论】:

        【解决方案7】:

        可以试试这个..

        string.xml

        <string name="colorText"><![CDATA[ Give your string here. Like, This sentence has a<b><font color=#FF0000>Red color</b> text.]]></string>
        

        ExampleClass.java

        TextView colorTextView = (TextView)findViewById(R.id.colorText);
        String customColorText = getResources().getString(R.string.colorText)
        colorTextView.setText(Html.fromHtml(customColorText));
        

        【讨论】:

          【解决方案8】:
          1. 在 values.xml 文件中创建颜色

              <color name ="red">#ff0000</color>
            
          2. 在您的 strings.xml 中执行类似的操作

              <string name="some_text">The next word is <font fgcolor="red">red</font> but that is all</string>
            

          我发现我不能直接在strings.xml中使用十六进制代码

          【讨论】:

          • 这只是因为“红色”是系统中预定义的颜色。您只能使用浅绿色、黑色、蓝色、紫红色、绿色、灰色、石灰、栗色、海军蓝、橄榄色、紫色、红色、银色、蓝绿色、白色和黄色。尝试从 colors.xml 修改您的 元素或将其完全删除;你会发现它没有任何区别。
          【解决方案9】:

          根据我的经验,我在 strings.xml 中存储的颜色看起来像

          &lt;color name="name_color"&gt;#ffffff&lt;/color&gt;

          当我有一个视图并设置`

          nameView.setBackgroundColor(R.color.name_color);

          没关系。

          但是当我为文本设置颜色时看起来像

          name_TextView.setTextColor(R.color.name_color);

          不是效果

          如果你遇到同样的问题,只需设置

          name_TextView.setTextColor(Color.parseColor("code hexa of color you want"));

          希望对您有所帮助。

          【讨论】:

            【解决方案10】:

            希望对您有所帮助。

               TextView textView=findViewById(R.id.text);
            
               String error= getResources().getString(R.string.error);
               ForegroundColorSpan fgColor=new ForegroundColorSpan(Color.RED);  // here set the color of Text
            
               SpannableString ss=new SpannableString(error);  //here set the text to spannableString
            
               ss.setSpan(fgColor,0,error.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //here which color , when to where you will set the color
               textView.setText(ss);
            

            什么是 Android 中的 SpannableString?

            Android SpannableString 示例。 android 中的 SpannableString 是在 TextView 中设置字符串样式的绝佳方式。简而言之,它允许 TextView 为不同的文本区域提供不同的样式。

            【讨论】:

              【解决方案11】:

              您可以通过string.xml更改文字颜色:

              <string name="completed_running_not_alloted"><font fgcolor="#6DC287">Completed /</font><font fgcolor="#FFCC29"> Running /</font> Not Alloted</string>
              

              如果您想以编程方式设置文本,那么您可以使用它 动态渲染文本颜色。

               private SpannableStringBuilder setSpan(int completed, int running){
                          SpannableStringBuilder span1 = new SpannableStringBuilder(completed+" / ");
                          ForegroundColorSpan color1=new ForegroundColorSpan(Color.parseColor("#6DC287"));
                          span1.setSpan(color1, 0, span1.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
              
                          SpannableStringBuilder span2 = new SpannableStringBuilder(running+"");
                          ForegroundColorSpan color2=new ForegroundColorSpan(Color.parseColor("#FFCC29"));
                          span2.setSpan(color2, 0, span2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
              
                          Spanned concatenated=(Spanned) TextUtils.concat(span1, span2);
              
                          return new SpannableStringBuilder(concatenated);
                      }
              

              【讨论】:

                【解决方案12】:

                在 Strings.xml 中:

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

                然后在代码中:

                 TextView myText = (TextView) findViewById(R.id.tvJugador1);
                  myText.setTextColor(R.color.mygreen);
                

                【讨论】:

                • 这会以编程方式设置颜色,而不是按要求在 xml 文件中。
                【解决方案13】:

                是否可以直接在string.xml中设置字符串的颜色?

                是的,有可能……

                不要使用十六进制代码或您在颜色中定义的颜色!在 Android Studio 中使用默认颜色。 RED WHITE BLACK 并自动建议这样的颜色。如果您使用十六进制代码,则在 api 19 之前的 api 中将看不到文本

                <string name="try"><font color="RED">This is blue color words </font> this default color words</string>
                

                对于 Android Stduio 其他 HTML 代码

                <string name="try2"><b>Bold style words</b> And this not bold ;) </string>
                <string name="try3"><u>Underline style words</u> And this not underline </string>
                <string name="try4"><i>italic style words</i> And this not underline </string>
                

                来源:https://developer.android.com/guide/topics/resources/string-resource.html#escaping_quotes

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-10-06
                  相关资源
                  最近更新 更多