java Android 资源文件夹中 res/color 和 res/values/colors.xml 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16951960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between res/color and res/values/colors.xml in Android resources folder
提问by Marek
Is there any reason, why in the resources folder we have two folders in which we can define colors? (according to android developer page http://developer.android.com/guide/topics/resources/providing-resources.html#ResourceTypes).
有什么原因,为什么在资源文件夹中我们有两个可以定义颜色的文件夹?(根据 android 开发者页面http://developer.android.com/guide/topics/resources/providing-resources.html#ResourceTypes)。
This is the quote from android developer page:
这是来自 android 开发者页面的引用:
values/
XML files that contain simple values, such as strings, integers, and colors.color/
XML files that define a state list of colors. See Color State List Resource
values/
XML 文件,包含简单的值,例如字符串、整数和颜色。color/
XML 文件,用于定义颜色状态列表。请参阅颜色状态列表资源
Is there any difference between Colors stored in res/colors and res/values? Which one is more preferable?
存储在 res/colors 和 res/values 中的颜色之间有什么区别吗?哪个更可取?
回答by Aurand
See Color State List Resource
请参阅颜色状态列表资源
Did you follow that link? http://developer.android.com/guide/topics/resources/color-list-resource.html
你关注那个链接了吗? http://developer.android.com/guide/topics/resources/color-list-resource.html
I think it answers your question.
我认为它回答了你的问题。
回答by TT--
By location,
按地点,
res/color/
Is for the resource that is compiled to datatype Resource pointer to a
ColorStateList
.
res/color/
用于编译为数据类型的资源 Resource 指向 a 的指针
ColorStateList
。
A
ColorStateList
is an object you can define in XML that you can apply as a color, but will actually change colors, depending on the state of the View object to which it is applied.syntax:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:color="hex_color" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_selected=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_window_focused=["true" | "false"] /> </selector>
A
ColorStateList
是您可以在 XML 中定义的对象,您可以将其应用为颜色,但实际上会更改颜色,具体取决于应用它的 View 对象的状态。句法:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:color="hex_color" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_selected=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_window_focused=["true" | "false"] /> </selector>
res/values/
If you want to provide a static color resource, use a simple Color value.
res/values/
如果要提供静态颜色资源,请使用简单的 Color 值。
- That is a color value defined in XML, specified with an RGB value and alpha channel.
- You can use a color resource any place that accepts a hexadecimal color value.
- You can also use a color resource when a drawable resource is expected in XML (for example,
android:drawable="@color/green"
).
- 这是在 XML 中定义的颜色值,用 RGB 值和 alpha 通道指定。
- 您可以在任何接受十六进制颜色值的地方使用颜色资源。
- 当 XML 中需要可绘制资源时,您还可以使用颜色资源(例如,
android:drawable="@color/green"
)。