在Android中,我尝试从res / values / colors.xml
设置颜色使用title.setBackgroundColor()
目前我可以使用:title.setBackgroundColor(Color.GREEN);
但是,我想引用.xml中的颜色
当我使用时:
title.setBackgroundColor(R.color.white);
指的是:
<color name="white">#FFFFFF</color>
Andriod工作室报道:
Should pass resolved color instead of resource id here: `getResources().getColor(R.color.white)` less... (⌘F1)
This inspection looks at Android API calls that have been annotated with various support annotations (such as RequiresPermission or UiThread) and flags any calls that are not using the API correctly as specified by the annotations. Examples of errors flagged by this inspection:
Passing the wrong type of resource integer (such as R.string) to an API that expects a different type (such as R.dimen).
Forgetting to invoke the overridden method (via super) in methods that require it
Calling a method that requires a permission without having declared that permission in the manifest
Passing a resource color reference to a method which expects an RGB integer value.
...and many more. For more information, see the documentation at http://developer.android.com/tools/debugging/annotations.html
答案 0 :(得分:1)
转到res-&gt; values-&gt; color.xml并编写如下代码:
colors.xml
<resources>
<color name="name_color">#3F51B5</color>
</resources>
并设置:
title.setBackgroundResource(R.color.name_color);
答案 1 :(得分:0)
在你的xml文件中:
<color name="newcolor">#FF00</color>
并使用
title.setBackgroundResource(R.color.newcolor);
答案 2 :(得分:0)
您需要使用ContextCompat.getColor(getContext(), R.color.green)
。这将通过您传递的ID为您提供所需的颜色。最后:
title.setBackgroundColor(ContextCompat.getColor(getContext, R.color.green));
答案 3 :(得分:0)
您可以在Xamarin中试用
SomeView.SetBackgroundResource(Resource.Color.justRed);
答案 4 :(得分:-1)
只需添加此
即可title.setBackground(getResources().getColor(R.color.yourcolor));
或
title.setBackgroundResource(R.color.yourColor);