我有一个自定义组件,我想让它和TextView使用相同的颜色。也就是说,我不想复制其颜色,而是想获取默认背景色和前景色(如果在Android上存在这样的概念)。
[编辑]
以下似乎会产生TextView的文本颜色。但这只是运气吗?对我来说,默认的TextView为什么要使用`android.R.attr.textColorSecondary`不太直观?为什么`resolveAttribute`不能直接返回颜色呢?
[编辑]
以下似乎会产生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
默认为“次要文本颜色”。
我想我还是有点幸运的,但我仍然不喜欢我那段小小的代码片段。