简体   繁体   中英

Change XML Background Programmatically - Xamarin.Android C#

I'm trying to change a Button background property programmatically using Xamarin.Android C# but can't get it done.

Xml file:

... <Button
                        p1:id="@+id/BtnGo"
                        p1:layout_width="wrap_content"
                        p1:layout_height="30dp"
                        p1:paddingHorizontal="15dp"
                        p1:text="Go"
                        p1:textColor="#ffffff"
                        p1:textSize="15dp"
                        p1:background="@drawable/togglefocus"
                        p1:textAllCaps="false"
                        p1:layout_gravity="center" /> ...

Need to change

p1:background="@drawable/togglefocus"

To

p1:background="@drawable/myfocus"

But on execution time / Programmatically (C#).

Tried this:

        Btn = FindViewById<Button>(Resource.Id.BtnGo);
        Btn.Click += delegate           
        {                
            Btn.Background("@drawable/myfocus"); //myfocus is an xml file in: Resources > Drawable
        };

Any Ideas?

If I am not wrong you can use the SetBackgroundResouce method for this:

  Btn = FindViewById<Button>(Resource.Id.BtnGo);
    Btn.Click += delegate           
    {   
       Btn.SetBackgroundResource(Resource.Drawable.myfocus);
    };

Good luck! Feel free to get back if you have queries!

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