简体   繁体   中英

How to get the background image of a button control in Xamarin Android c#?

I am using a button control. I want get the android:background (image source) from code behind. Based on that I will continue with my code. How can I get the image source from code behind?

<Button
android:id="@+id/btnPlayPause"
android:layout_width="34dp"
android:layout_height="34dp"
android:layout_marginLeft="15dp"
android:visibility="gone"
android:background="@drawable/play"/>

Are you looking for something like this?

Button btn = new Button(Context);
Drawable btnDrawable = btn.Background;

now you can compare the drawable with others (for example from Android.Resource)

You can try this code

var beachImage = new Image { Aspect = Aspect.AspectFit };

btnPlayPause.Image = ImageSource.FromFile("imageName.png");

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