Xamarin Android中SetBackgroundDrawable已被弃用,但不建议使用SetBackground()。

12
var textView = parentView.FindViewById<TextView>(Resource.Id.txt_chat_message);
GradientDrawable gd = new GradientDrawable();
gd.SetCornerRadius(10);
gd.SetColor(Color.Yellow);
textView.SetBackgroundDrawable(gd);
与上面的例子一样,SetBackgroundDrawable 允许我以编程方式控制颜色和半径。我查看了 SetBackgroundResource,但是我找不到一个清晰的示例,因为它似乎只接受指向资源的ID,而我无法在程序中更改它。请问有人能帮助我提供一种替代方法,使我能够像上面的SetBackgroundDrawable一样灵活地进行相同的操作吗?
3个回答

20

使用Background属性。通常情况下,当Android具有没有参数的getX/setX方法时,Xamarin会将其转换为名为X的C#风格属性。

var textView = parentView.FindViewById<TextView>(Resource.Id.txt_chat_message);
GradientDrawable gd = new GradientDrawable();
gd.SetCornerRadius(10);
gd.SetColor(Color.Yellow);
textView.Background = gd;

但是仅使用“Background”无法更改半径(除非我很傻,这完全有可能:) - Matthew Barnden
背景仍然是一个Drawable,因此您应该能够像以前一样使用相同的方法。请查看我的编辑。 - Jason
我不确定这是否是Xamarin的问题,但你的编辑会导致程序崩溃。现在已经有点晚了,所以我明天会尝试调试并找出原因,感谢你的帮助,非常感激。 - Matthew Barnden
这是我在尝试按照您的建议操作时输出中收到的错误信息:java.lang.NoSuchMethodError: no method with name='setBackground' signature='(Landroid/graphics/drawable/Drawable;)V' in class Landroid/view/View; - Matthew Barnden
我的模拟器运行在3.1上,需要升级到4.12或更高版本才能正常工作,非常感谢。有关详细信息,请参见此报告https://bugzilla.xamarin.com/show_bug.cgi?id=26624。 - Matthew Barnden
对于其他人,我使用了以下版本检查:BuildVersionCodes sdk = Build.VERSION.SdkInt; if (sdk < BuildVersionCodes.JellyBean) { TextView.SetBackgroundDrawable(gd); } else { TextView.Background = gd; } - Matthew Barnden

4

不要使用SetBackgroundDrawable(back);,而是使用Background = back;

你需要这样做:

back = context.GetDrawable(Resource.Drawable.cover);
// SetBackgroundDrawable(back);
Background = back;`

3

编辑

根据在设备上运行它的结果,目前(2015年11月11日)看来Background属性并没有完全实现。我的试错方法表明,通过属性设置Background会抛出一个异常,因为它找不到适当参数的setBackground方法。因此,问题不在于获取可绘制的新方式,而在于尝试设置它们时出现了问题。也许我使用方式不正确,所以我愿意接受更正。

//Works
yesButton.SetBackgroundDrawable(ContextCompat.GetDrawable(context, Resource.Drawable.selector_green_button));

//Works
yesButton.SetBackgroundDrawable(ResourcesCompat.GetDrawable(Resources, Resource.Drawable.selector_green_button, Resources.NewTheme()));

//Doesn't Work     
yesButton.Background = ResourcesCompat.GetDrawable(Resources, Resource.Drawable.selector_green_button, Resources.NewTheme());

//Doesn't Work
yesButton.Background = ContextCompat.GetDrawable(context, Resource.Drawable.selector_green_button);

//Doesn't Work
yesButton.Background = Resources.GetDrawable(Resource.Drawable.selector_green_button);

原始答案

您可以像@Jason建议的那样使用Background属性。

为了使用它,您现在需要获取Drawable,现在是有趣的部分:

GetDrawable方法已弃用(自API 22以来),因此您应该使用:

getContext().getDrawable()

someControl.Background = ContextCompat.GetDrawable(context, Resource.Drawable.your_drawable);

替换 Resources.GetDrawable(Resource.Drawable.your_drawable);

找到了这个链接作为参考:有关在Android本地获取可绘制对象的更多信息


您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Matthew Barnden
我刚在模拟器上运行了它,但没有在设备上测试,明天在工作中会测试(需要使用using Android.Support.V4.Content),但是VS会让你知道的 :) - kirotab

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接