gpt4 book ai didi

android - 从资源 id 整数获取颜色给出错误

转载 作者:行者123 更新时间:2023-11-30 05:07:24 26 4
gpt4 key购买 nike

我创建了一个自定义 View ,它接受背景颜色 resid 和阴影颜色 resId。但是当我转换资源 ID 以将它们存储在一个 int 变量中时。它给出错误为

Expected a color resource id (R.color.) but received an RGB integer 

以下是我的setter方法

    @Override
public void setBackgroundColor(@ColorRes int backgroundResId) {
this.backgroundColor = ContextCompat.getColor(context, backgroundResId);
}

public void setShadowColor(@ColorRes int shadowColorId) {
this.shadowColor = ContextCompat.getColor(context, shadowColorId);
}

使用时显示同样的错误

public void setBackgroundColor(@ColorRes int backgroundResId) {
this.backgroundColor = context.getResources().getColor(backgroundResId);
}

最佳答案

我想您在方法 setBackgroundColor 中传递了错误的参数。你应该这样使用它:

//some code
setBackgroundColor(R.color.colorPrimary)
//some code

关于android - 从资源 id 整数获取颜色给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54341542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com