Xamarin Android SetBackgroundDrawable deprecated but not SetBackground ()

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

As in the example above SetBackgroundDrawable, I can programmatically control the color and radius. I looked at SetBackgroundResouce, but I can’t find a clear example, because it just seems to take an identifier to a resource that I could not change proportionally.

Can someone help me provide an alternative that gives me the opportunity to do the same as SetBackgroundDrawableabove, please?

+4
source share
3 answers

Background. , Android getX/setX , Xamarin # X.

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

Edit

, Background (11.11.2015). , 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);

Background @Jason, .

, , Drawable :

GetDrawable ( API 22 ), :

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

Resources.GetDrawable(Resource.Drawable.your_drawable);

: Android

+1

SetBackgroundDrawable(back);Use insteadBackground = back;

You need to do the following:

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

Source: https://habr.com/ru/post/1615288/


All Articles