在 strings.xml 中存储十六进制颜色值

Posted

技术标签:

【中文标题】在 strings.xml 中存储十六进制颜色值【英文标题】:Storing hex color values in strings.xml 【发布时间】:2011-10-04 04:32:21 【问题描述】:

我正在尝试将我的文本的 hex color 值存储在 strings.xml 中,因此所有布局文件都将引用它(以便能够轻松快速更改项目的所有布局文本)但是我有麻烦参考一下。

在我的 xml 布局中使用 android:textColor="#FFFFFF" 效果很好。但是,使用android:textColor="@strings/textColor" 在包含# 和不包含时都会出现错误。

当我不包含 # 时,它会要求 #。当我添加 # DDMS 报告时:

07-13 04:35:22.870:错误/AndroidRuntime(331):原因:android.content.res.Resources$NotFoundException:来自可绘制资源 ID #0x7f040003 的文件 #FF0000:需要 .xml 扩展名

有谁知道如何在布局文件中组合语句?例如textColor="#"+"@strings/textColor,然后将字符串设置为"FFFFFF"

【问题讨论】:

【参考方案1】:

您需要在您的 xml 中创建一组样式(通常在 res/values/styles.xml 中)

<color name="gray">#eaeaea</color>
<color name="titlebackgroundcolor">#00abd7</color>
<color name="titlecolor">#666666</color>

在布局文件中你可以调用颜色或样式:

android:textColor="@color/titlecolor"

查看一些示例:

http://developer.android.com/guide/topics/ui/themes.html

【讨论】:

你是绝对的传奇。非常感谢。我之前肯定已经读过很多次了,但是在早上 5.55 时,我的记忆力并没有达到最佳状态。【参考方案2】:

您可以声明显式颜色资源。按照惯例,这些都放在 res/values/colors.xml 中:

<resources>
    <color name="my_color">#FFFFFFFF</color>
</resources>

然后在布局或其他地方你可以写android:textColor="@color/my_color"

【讨论】:

【参考方案3】:

为此,您可以做一件事:在 values 文件夹中定义 colors.xml,然后使用 # 添加您的颜色值,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="myColor">#000000</color>
</resources>

那你如何在应用中引用这个颜色值:

静态:

android:textColor="@color/myColor"

编码:

TextView1.setTextColor(R.color.myColor);

【讨论】:

【参考方案4】:

使用这些颜色

<color name="Maroon">#800000</color>
<color name="Aquamarine">#7FFFD4</color>
<color name="Chartreuse">#7FFF00</color>
<color name="LawnGreen">#7CFC00</color>
<color name="MediumSlateBlue">#7B68EE</color>
<color name="LightSlateGray">#778899</color>
<color name="SlateGray">#708090</color>
<color name="OliveDrab">#6B8E23</color>
<color name="SlateBlue">#6A5ACD</color>
<color name="DimGray">#696969</color>
<color name="MediumAquamarine">#66CDAA</color>
<color name="CornflowerBlue">#6495ED</color>
<color name="CadetBlue">#5F9EA0</color>
<color name="DarkOliveGreen">#556B2F</color>
<color name="Indigo">#4B0082</color>
<color name="MediumTurquoise">#48D1CC</color>
<color name="DarkSlateBlue">#483D8B</color>
<color name="SteelBlue">#4682B4</color>
<color name="RoyalBlue">#4169E1</color>
<color name="Turquoise">#40E0D0</color>
<color name="MediumSeaGreen">#3CB371</color>
<color name="LimeGreen">#32CD32</color>
<color name="DarkSlateGray">#2F4F4F</color>
<color name="SeaGreen">#2E8B57</color>
<color name="ForestGreen">#228B22</color>
<color name="LightSeaGreen">#20B2AA</color>
<color name="DodgerBlue">#1E90FF</color>
<color name="MidnightBlue">#191970</color>
<color name="Aqua">#00FFFF</color>
<color name="Cyan">#00FFFF</color>
<color name="SpringGreen">#00FF7F</color>
<color name="Lime">#00FF00</color>
<color name="MediumSpringGreen">#00FA9A</color>
<color name="DarkTurquoise">#00CED1</color>
<color name="DeepSkyBlue">#00BFFF</color>
<color name="DarkCyan">#008B8B</color>
<color name="Teal">#008080</color>
<color name="Green">#008000</color>
<color name="DarkGreen">#006400</color>
<color name="Blue">#0000FF</color>
<color name="MediumBlue">#0000CD</color>
<color name="DarkBlue">#00008B</color>
<color name="Navy">#000080</color>
<color name="Black">#000000</color>
<color name="Transparent">#00000000</color>


<drawable name="screen_background_light">#ffffffff</drawable>
<drawable name="screen_background_dark">#ff000000</drawable>
<drawable name="status_bar_closed_default_background">#ff000000</drawable>
<drawable name="status_bar_opened_default_background">#ff000000</drawable>
<drawable name="notification_item_background_color">#ff111111</drawable>
<drawable name="notification_item_background_color_pressed">#ff454545</drawable>
<drawable name="search_bar_default_color">#ff000000</drawable>
<drawable name="safe_mode_background">#60000000</drawable>

<drawable name="screen_background_dark_transparent">#80000000</drawable>

<drawable name="screen_background_light_transparent">#80ffffff</drawable>

<color name="safe_mode_text">#80ffffff</color>
<color name="white">#ffffffff</color>
<color name="black">#ff000000</color>
<color name="transparent">#00000000</color>
<color name="background_dark">#ff000000</color>
<color name="background_light">#ffffffff</color>
<color name="bright_foreground_dark">@android:color/background_light</color>
<color name="bright_foreground_light">@android:color/background_dark</color>
<color name="bright_foreground_dark_disabled">#80ffffff</color>
<color name="bright_foreground_light_disabled">#80000000</color>
<color name="dim_foreground_dark">#bebebe</color>
<color name="dim_foreground_dark_disabled">#80bebebe</color>
<color name="dim_foreground_dark_inverse">#323232</color>
<color name="dim_foreground_dark_inverse_disabled">#80323232</color>
<color name="hint_foreground_dark">#808080</color>
<color name="dim_foreground_light">#323232</color>
<color name="dim_foreground_light_disabled">#80323232</color>
<color name="dim_foreground_light_inverse">#bebebe</color>
<color name="dim_foreground_light_inverse_disabled">#80bebebe</color>
<color name="hint_foreground_light">#808080</color>
<color name="highlighted_text_dark">#9983CC39</color>
<color name="highlighted_text_light">#9983CC39</color>
<color name="link_text_dark">#5c5cff</color>
<color name="link_text_light">#0000ee</color>
<color name="suggestion_highlight_text">#177bbd</color>


<color name="input_method_navigation_guard">#ff000000</color>

<!-- For date picker widget -->
<drawable name="selected_day_background">#ff0092f4</drawable>

<!-- For settings framework -->
<color name="lighter_gray">#ddd</color>
<color name="darker_gray">#aaa</color>

<!-- For security permissions -->
<color name="perms_dangerous_grp_color">#33b5e5</color>
<color name="perms_dangerous_perm_color">#33b5e5</color>
<color name="shadow">#cc222222</color>
<color name="perms_costs_money">#ffffbb33</color>

<!-- For search-related UIs -->
<color name="search_url_text_normal">#7fa87f</color>
<color name="search_url_text_selected">@android:color/black</color>
<color name="search_url_text_pressed">@android:color/black</color>

<!-- SlidingTab -->
<color name="sliding_tab_text_color_active">@android:color/black</color>
<color name="sliding_tab_text_color_shadow">@android:color/black</color>

<!-- keyguard tab -->
<color name="keyguard_text_color_normal">#ffffff</color>
<color name="keyguard_text_color_unlock">#a7d84c</color>
<color name="keyguard_text_color_soundoff">#ffffff</color>
<color name="keyguard_text_color_soundon">#e69310</color>
<color name="keyguard_text_color_decline">#fe0a5a</color>

<!-- keyguard clock -->
<color name="lockscreen_clock_background">#ffffffff</color>
<color name="lockscreen_clock_foreground">#ffffffff</color>
<color name="lockscreen_clock_am_pm">#ffffffff</color>
<color name="lockscreen_owner_info">#ff9a9a9a</color>

<!-- keyguard overscroll widget pager -->
<color name="kg_multi_user_text_active">#ffffffff</color>
<color name="kg_multi_user_text_inactive">#ff808080</color>
<color name="kg_widget_pager_gradient">#ffffffff</color>

<!-- FaceLock -->
<color name="facelock_spotlight_mask">#CC000000</color>

<!-- For holo theme -->
<drawable name="screen_background_holo_light">#fff3f3f3</drawable>
<drawable name="screen_background_holo_dark">#ff000000</drawable>

<color name="background_holo_dark">#ff000000</color>
<color name="background_holo_light">#fff3f3f3</color>
<color name="bright_foreground_disabled_holo_dark">#ff4c4c4c</color>
<color name="bright_foreground_disabled_holo_light">#ffb2b2b2</color>
<color name="dim_foreground_holo_dark">#bebebe</color>
<color name="dim_foreground_disabled_holo_dark">#80bebebe</color>
<color name="dim_foreground_inverse_holo_dark">#323232</color>
<color name="dim_foreground_inverse_disabled_holo_dark">#80323232</color>
<color name="hint_foreground_holo_dark">#808080</color>
<color name="dim_foreground_holo_light">#323232</color>
<color name="dim_foreground_disabled_holo_light">#80323232</color>
<color name="dim_foreground_inverse_holo_light">#bebebe</color>
<color name="dim_foreground_inverse_disabled_holo_light">#80bebebe</color>
<color name="hint_foreground_holo_light">#808080</color>
<color name="highlighted_text_holo_dark">#6633b5e5</color>
<color name="highlighted_text_holo_light">#6633b5e5</color>
<color name="link_text_holo_dark">#5c5cff</color>
<color name="link_text_holo_light">#0000ee</color>

<!-- Group buttons -->
<eat-comment/>

<color name="group_button_dialog_pressed_holo_dark">#46c5c1ff</color>
<color name="group_button_dialog_focused_holo_dark">#2699cc00</color>
<color name="group_button_dialog_pressed_holo_light">#ffffffff</color>
<color name="group_button_dialog_focused_holo_light">#4699cc00</color>

<!-- Highlight colors for the legacy themes -->
<eat-comment/>

<color name="legacy_pressed_highlight">#fffeaa0c</color>
<color name="legacy_selected_highlight">#fff17a0a</color>
<color name="legacy_long_pressed_highlight">#ffffffff</color>

<!-- General purpose colors for Holo-themed elements -->
<eat-comment/>

<!-- A light Holo shade of blue -->
<color name="holo_blue_light">#ff33b5e5</color>
<!-- A light Holo shade of gray -->
<color name="holo_gray_light">#33999999</color>
<!-- A light Holo shade of green -->
<color name="holo_green_light">#ff99cc00</color>
<!-- A light Holo shade of red -->
<color name="holo_red_light">#ffff4444</color>
<!-- A dark Holo shade of blue -->
<color name="holo_blue_dark">#ff0099cc</color>
<!-- A dark Holo shade of green -->
<color name="holo_green_dark">#ff669900</color>
<!-- A dark Holo shade of red -->
<color name="holo_red_dark">#ffcc0000</color>
<!-- A Holo shade of purple -->
<color name="holo_purple">#ffaa66cc</color>
<!-- A light Holo shade of orange -->
<color name="holo_orange_light">#ffffbb33</color>
<!-- A dark Holo shade of orange -->
<color name="holo_orange_dark">#ffff8800</color>
<!-- A really bright Holo shade of blue -->
<color name="holo_blue_bright">#ff00ddff</color>
<!-- A really bright Holo shade of gray -->
<color name="holo_gray_bright">#33CCCCCC</color>

<drawable name="notification_template_icon_bg">#3333B5E5</drawable>
<drawable name="notification_template_icon_low_bg">#0cffffff</drawable>

<!-- Keyguard colors -->
<color name="keyguard_avatar_frame_color">#ffffffff</color>
<color name="keyguard_avatar_frame_shadow_color">#80000000</color>
<color name="keyguard_avatar_nick_color">#ffffffff</color>
<color name="keyguard_avatar_frame_pressed_color">#ff35b5e5</color>
<color name="accessibility_focus_highlight">#80ffff00</color>


<color name="header_color">#016435</color>
<color name="back_ground_color">#008000</color>

<color name="new_header_color">#3bb39d</color>

<color name="dialog_title_color">#fcbc30</color>

【讨论】:

【参考方案5】:

### 您的代码在 string.xml 文件中###

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

在您的代码中,您声明颜色名称为“textColor”,我想向您确认文本颜色给出的名称是内置方法,但您不能覆盖内置方法。

您的问题的解决方案是,您的代码应该是这样的:

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

在这种情况下,颜色名称不是内置方法,您没有遇到任何运行时错误。

谢谢。

【讨论】:

以上是关于在 strings.xml 中存储十六进制颜色值的主要内容,如果未能解决你的问题,请参考以下文章

在 Strings.xml 与代码中存储字符串是不是会使逆向工程变得更加乏味?

使用strings.xml 在数据库上存储多语言值的最佳方法?

Android中strings.xml字符串中的粗体字

是否可以使用 SQLite 来存储翻译而不是像 strings.xml 这样的 XML 资源

前端开发 - JQuery - 中

从jpeg中提取十六进制颜色,存储在data.frame中,然后使用ggplot进行绘图