我有一个Image(image1)和一个按钮中的文本,我需要更改图像 单击时(图像2)。怎么办?
答案 0 :(得分:0)
试试这个
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btn.setBackgroundResource(R.drawable.your_image);
}
});
答案 1 :(得分:0)
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);
如果要设置多个drawable,可以通过以下方式保留和更改其中一些:
var drawables = button.GetCompoundDrawables();
button.SetCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], Resources.GetDrawable(Resource.Drawable.button_background, this.Theme), drawables[3]);