如何在Android上获取默认颜色

13
我有一个自定义组件,我想让它和TextView使用相同的颜色。也就是说,我不想复制其颜色,而是想获取默认背景色和前景色(如果在Android上存在这样的概念)。
[编辑]
以下似乎会产生TextView的文本颜色。但这只是运气吗?对我来说,默认的TextView为什么要使用`android.R.attr.textColorSecondary`不太直观?为什么`resolveAttribute`不能直接返回颜色呢?
TypedValue tv = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.textColorSecondary, tv, true);
Color holyColor = getResources().getColor(tv.resourceId);

[编辑]

我在android.git.kernel.org找到了TextView的源代码,但是其中包含了很多关于com.android.internal.R的引用,我觉得这些在我的代码中不应该使用。我正在寻找一些证据来表明TextView使用android.R.attr.textColorSecondary

[编辑]

我在developer.android.com上找到了一些证据,在styles.xml中,TextView使用android.R.attr.textAppearanceSmall。文档说明textAppearanceSmall默认为“次要文本颜色”。

我想我还是有点幸运的,但我仍然不喜欢我那段小小的代码片段。


1个回答

0
你需要寻找的是属性。属性将小部件与样式链接起来。例如,android:background 是您为特定视图设置的内容,但还有像 android:panelBackgroundandroid:windowBackground 这样的属性,您可以使用它们来覆盖整个系统。
您应该查看 R.attr,然后在您的小部件中链接到这些属性。其中许多属性都与 TextView 相关;下载 Android 源代码并查看使用了哪些属性是有意义的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接