首页 > 解决方案 > 如何在android中使用主题颜色

问题描述

我有一个屏幕,列出了我所有的海关主题。我想在其名称旁边显示主题的颜色。所以我需要未使用的(不是当前的)主题颜色。

通常,我们可以这样做,

Resources.Theme theme = context.getTheme();

theme.resolveAttribute(themeColor, typedValue, true);

但在这种情况下,我必须写这样的东西getTheme(R.style.foo)

我该怎么办?

标签: androidandroid-styles

解决方案


经过大量搜索,我意识到,这不是关于主题,而是关于风格。真丢人。所以现在,由于打开了这个问题,我回答了我的问题。

首先你必须定义你想要什么

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();

推荐阅读