android - 如何在android中使用主题颜色
问题描述
我有一个屏幕,列出了我所有的海关主题。我想在其名称旁边显示主题的颜色。所以我需要未使用的(不是当前的)主题颜色。
通常,我们可以这样做,
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();
推荐阅读
- sql - Db2 SQL 查询特有的错误
- python - web3.exceptions.ValidationError:带有元组[],地址,字节
- python - 如何通过填充正确遮盖多个重叠图?
- html - 在移动设备上,我的网站的主要内容向右溢出太远了
- javascript - React:指定嵌套子组件的位置?
- node.js - PDF 到节点中的图像
- php - 如何显示自定义帖子类型的自定义字段
- tensorflow - tensorFlowJs Json 模型问题
- javascript - 如何在 css 上创建这个视图?还是JavaScript?
- azure - Azure 策略修改效果