我有一个列出所有我的海关主题的屏幕。我想在主题名称旁边显示主题的颜色。因此,我需要未使用的(不是当前的)主题颜色。
通常,我们可以这样操作,
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(themeColor, typedValue, true);
但是在这种情况下,我必须写类似的内容getTheme(R.style.foo)
。
我该如何搭配?
经过大量的搜索,我意识到了这一点,这与主题无关,而与样式有关。真可惜现在,由于打开了这个问题,我回答了我的问题。
首先,您必须定义想要的内容
int[] attrs = {R.attr.color_1, R.attr.color_2, R.attr.color_3, R.attr.color_4};
然后您可以按索引获取它们
TypedArray ta = holder.itemView.getContext().obtainStyledAttributes(R.style.AppTheme_Pink, attrs);
ViewHelpers.Companion.changeBackgroundSolidColor(holder.color1ImageView, ta.getColor(0, Color.GRAY));
ViewHelpers.Companion.changeBackgroundSolidColor(holder.color2ImageView, ta.getColor(1, Color.GRAY));
ViewHelpers.Companion.changeBackgroundSolidColor(holder.color3ImageView, ta.getColor(2, Color.GRAY));
ViewHelpers.Companion.changeBackgroundSolidColor(holder.color4ImageView, ta.getColor(3, Color.GRAY));
不要忘记回收站
ta.recycle();