我需要获取 Android 用于绘制 ListView 和 EditText 选择的选择颜色。我知道这些控件用户选择器来绘制它们的状态,但我已经编写了一些小部件,我想要匹配它们正在运行的平台的选择颜色,并且定义的可绘制对象无法做到这一点,因为它们使用的是 9修补图像而不是颜色。
我浏览了整个 Android 源代码,但没有找到可以用来获取我正在寻找的颜色的颜色选择器或颜色常量。
I need to get the selection color used by Android to draw ListView and EditText selection. I know these controls user selectors to draw their states, but I've written a few widgets that I want to match the selection color of the platform they are running on and the drawables that are defined can't do that because they are using 9 patch images instead of colors.
I've looked all through the Android source and haven't found a color selector or color constant I can use to get the color I'm looking for.
发布评论
评论(5)
你可以试试这个:
You could try this :
您可以从这里获取所有资源
,也可以直接使用这样
或这样
you can get all the resource from here
or you can use directly like this
or like this also
看看这个答案。您可以浏览平台颜色 和 样式。请注意,对于每个 Android 版本,此值可能有所不同。如果您想使用平台样式,请不要为控件创建自己的自定义选择器。希望它会有所帮助。
Take a look at this answer. You can browse through the platform colors and styles at the Android github mirror. Please note this values can be different for every version of Android. If you want to use platform styles, just don't create your own custom selectors for the controls. Hope it will help.
您只需获取
list_selector_background
可绘制对象,如 Jignesh 所解释的那样,然后找到其平均颜色,如 这个答案(我会在你的初始化代码中这样做,这样你就不必每次都浪费时间处理它们,但是嘿,这还为时过早 优化)。这应该与主题足够一致,以便您的小部件根据需要进行匹配。您的代码可能如下所示:
You could just get the
list_selector_background
drawable, as explained by Jignesh, and then find its average color as shown in this answer (I'd do it in your initialization code so you don't have to waste processing them every time, but hey, that's premature optimization). That should be consistent enough with the theme to let your widgets match as needed.Your code could look like this:
对于谷歌拥有的设备,您可以轻松实现此目的,但对于其他制造商则不然,因为大多数制造商已经覆盖了几乎所有 Android 版本(包括果冻豆)的默认颜色、布局、背景等。
因此,理想情况下不推荐这样做,而且很难遵循每个人的设计指南。
You can achieve easily achieve this for google owned devices but not for other manufacturers as most of the manufacturers have overridden the default colors, layouts, backgrounds, etc for almost all the versions of android including jelly-beans.
So ideally its not recommended and also tough to follow each and everyone's design guidelines.