簡體   English   中英

以編程方式更改 XML 背景 - Xamarin.Android C#

[英]Change XML Background Programmatically - Xamarin.Android C#

我正在嘗試使用Xamarin.Android C# 以編程方式更改 Button background屬性,但無法完成。

xml文件:

... <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" /> ...

需要改變

p1:background="@drawable/togglefocus"

p1:background="@drawable/myfocus"

但是在執行時間/以編程方式(C#)。

試過這個:

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

有任何想法嗎?

如果我沒有錯,您可以為此使用 SetBackgroundResouce 方法:

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

祝你好運! 如果您有任何疑問,請隨時回來!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM