简体   繁体   中英

Get Color from resource id integer gives error

I have created a custom view which accepts background color resid and shadow color resId. But when i convert resource ids to store them in a int variable. It give error as

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

Following are my setter methods

    @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);
    }

Same error is shown when used

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

I suppose you pass wrong argument in your method setBackgroundColor . You should use it in that way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM