Xamarin/C# 方式:
var button = FindViewById<Button>(Resource.Id.myButton);
button.Click += delegate {
button.Text = "Some New Text";
// If your image is a Drawable
button.SetBackgroundResource(Resource.Drawable.button_background);
// From an asset
button.Background = Drawable.CreateFromStream(Assets.Open("button_asset.jpg"), null);
// From an arbitrary path
button.Background = Drawable.CreateFromPath("/sdcard/Download/downloaded_file.jpg");
};
更新:
如果您在布局中使用drawableLeft、drawableRight 等...,您可以通过SetCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom) 更改这些:
button.SetCompoundDrawablesWithIntrinsicBounds(0, 0, Resource.Drawable.button_background, 0);
如果您要设置多个可绘制对象,您可以通过以下方式保留和更改其中一些:
var drawables = button.GetCompoundDrawables();
button.SetCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], Resources.GetDrawable(Resource.Drawable.button_background, this.Theme), drawables[3]);