android - 如何将字符串颜色更改为颜色资源?
问题描述
我想改变我的标题背景颜色,我正在使用 MaterialDrawer 库:
AccountHeader headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(Color.parseColor(mPrefs.getString("theme_color",getResources().getString(R.string.default_color)))
.withSelectionListEnabledForSingleProfile(false)
... and so on
但是对于标题背景,我收到错误消息“预期颜色资源 id (R.color.) 但收到一个 RGB 整数”并且无法设置从首选项读取的背景颜色。它在日志上给出错误:“ android.content.res.Resources$NotFoundException: Resource ID #0x2e60e8 ”
解决方案
从您的字符串创建 ColorDrawable,您可以将其设置为您的标题
int col = Color.parseColor(mPrefs.getString("theme_color",getResources().getString(R.string.default_color)));
ColorDrawable cd = new ColorDrawable();
cd.setColor(col);
headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withCompactStyle(false)
.withHeaderBackground(cd)
.withSavedInstance(savedInstanceState)
.build();
推荐阅读
- oracle - 如何在oracle存储过程中添加列并用值填充它
- android-espresso - Picasso 使用 Espresso 加载测试图像
- javascript - 无法附加到 chrome 插件的 popup.html 中的元素
- c# - 如何检查 C# 中哪个枚举器为空?
- javascript - 尝试使用 @alias 时,Jest 无法找到 @babel/code-frame
- javascript - 基本计算器,如何使按钮输入值到文本字段
- javascript - Tic Tac Toe alpha-beta MiniMaxfor 4x4 board (JavaScript)
- java - 为什么类型 LinkedList 不带参数?
- ruby - 一次在字符串中插入多个字符
- python - 像在 python 中一样在 C++ 中迭代