简体   繁体   English

如何在Android上获取默认颜色

[英]How to get the default colors on Android

I have a custom component that I want to give the same colors as a TextView. 我有一个自定义组件,我想提供与TextView相同的颜色。 That is, I don't want to copy its colors, I want to get the default background and foreground colors, if there's such a concept on android. 也就是说,我不想复制它的颜色,我想获得默认的背景和前景颜色,如果在Android上有这样的概念。

[Edit] [编辑]

The following seems to yield the text color of my TextView. 以下似乎产生我的TextView的文本颜色。 But is it just luck? 但这只是运气吗? It's not intuitive to me that a default TextView would use android.R.attr.textColorSecondary ? 对我来说,默认的TextView会使用android.R.attr.textColorSecondary对我来说不直观吗? And why does not resolveAttribute return the color directly? 为什么resolveAttribute不直接返回颜色?

TypedValue tv = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.textColorSecondary, tv, true);
Color holyColor = getResources().getColor(tv.resourceId);

[Edit] [编辑]

I found the source code of TextView at android.git.kernel.org , but it seemed to contain a lot of referrences to com.android.internal.R , which I don't think I should use in my own code. 我在android.git.kernel.org找到了TextView的源代码,但它似乎包含了很多com.android.internal.R引用,我认为我不应该在我自己的代码中使用它。 I'm currently looking for some kind of evidence that TextView uses android.R.attr.textColorSecondary . 我目前正在寻找TextView使用android.R.attr.textColorSecondary某种证据。

[Edit] [编辑]

I found some kind of evidence at developer.android.com , in styles.xml that TextView uses android.R.attr.textAppearanceSmall . 我在developer.android.com上找到了某种证据,在styles.xml中TextView使用了android.R.attr.textAppearanceSmall textAppearanceSmall is documented to default to "secondary text color". textAppearanceSmall记录为默认为“辅助文本颜色”。

I guess I was lucky after all, but I still don't like that little code snippet of mine. 我想我毕竟很幸运,但我仍然不喜欢我的那个小代码片段。

What you're looking for are attributes. 您正在寻找的是属性。 Attributes link widgets to styles. 属性将小部件链接到样式。 For example, android:background is what you'd set for a particular view, but there's attributes like android:panelBackground and android:windowBackground that you can use override to affect the system as a whole. 例如, android:background是你为特定视图设置的,但是有像android:panelBackgroundandroid:windowBackground这样的属性,你可以使用覆盖来影响整个系统。

You should look through R.attr and then link to those attributes in your widget. 您应该查看R.attr ,然后链接到窗口小部件中的这些属性。 There should be a number of them that are linked to TextView; 应该有许多链接到TextView的; it would make sense to download the Android source code and see which attributes are used. 下载Android源代码并查看使用的属性是有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM