android - 如何将字符串颜色转换为颜色资源?

标签 android material-design

我想更改页眉背景颜色并且我正在使用 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();

关于android - 如何将字符串颜色转换为颜色资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53063752/

相关文章:

android - 如何计算Google Play商店中某个应用的下载次数

android - 使用稀松布或颜色叠加更改 CollapsingToolbarLayout 中标题的背景颜色

web - 如何将 Material 设计应用于文件输入标签?

android - 为什么 Material Design Toolbar 有这种奇怪的填充?

android - node js和appium有什么关系?如何在两个不同的端口上运行到多个 appium 服务器实例?

java - 如何管理对象列表?

Android Fragment 事务在后台

java - 运行我的应用程序时出错

android - NumberPicker Material Style 自定义选择 Divider

android - 如何在 Android 中使用 RecyclerView 应用 setItemChecked(position,true)?