无法从int转换为Android.Graphics.Color

时间:2016-04-15 21:51:44

标签: android xamarin

在我的Resources--> Values--> colors.xml

<color name="myview_background">#EDDDDD</color>

我正在尝试按如下方式分配颜色:

 holder.ItemView.SetBackgroundColor(Resource.Color.myview_background);

我收到以下错误,我想知道如何修复?

  

无法从int转换为Android.Graphics.Color

4 个答案:

答案 0 :(得分:6)

试试这个:

holder.ItemView.SetBackgroundColor(_activity.Resources.GetColor(Resource.Color.mainview_background));

答案 1 :(得分:3)

请注意,Resources.GetColor(int)deprecated for Android API level 23(Marshmallow)。

标准

因此,如果您想获得23或更高的API的颜色资源,您还需要另外包含用于设置颜色属性样式的主题:

if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
    holder.ItemView.SetBackgroundColor(Resources.GetColor(Resource.Color.mainview_background, Theme));
}
else
{
    holder.ItemView.SetBackgroundColor(Resources.GetColor(Resource.Color.mainview_background));
}

v4支持库

如果您正在使用support libraries (version 23+),则可以使用ContextCompat.getColor(context, R.color.your_color);

holder.ItemView.SetBackgroundColor(new Android.Graphics.Color(Android.Support.V4.Content.ContextCompat.GetColor(Resource.Color.mainview_background)));

答案 2 :(得分:2)

这对我来说效果更好:

view.SetBackgroundResource(Resource.Color.my_color);

仅仅因为我不需要获得实际颜色,如果你想使用图像或任何类型的drawable,它也有效:

view.SetBackgroundResource(Resource.Drawable.my_drawable);

答案 3 :(得分:1)

这是我通常的做法!

Traceback (most recent call last):
  File "/home/eadaradhiraj/Documents/plag/venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/eadaradhiraj/Documents/plag/venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/eadaradhiraj/Documents/plag/venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/eadaradhiraj/Documents/plag/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/eadaradhiraj/Documents/plag/venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/eadaradhiraj/Documents/plag/venv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/eadaradhiraj/Documents/plag/venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/eadaradhiraj/Documents/plag/venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/eadaradhiraj/Documents/plag/samp_app/app/routes.py", line 39, in view_doc
    k = int(session.get('k'))