我需要从另一个按钮
为我的按钮设置背景资源我正在尝试,但它不起作用
button1.setBackground(button2.getBackground());
答案 0 :(得分:0)
试试此代码
final Drawable backgroundDrawable = button2.getBackground();
if (backgroundDrawable instanceof BitmapDrawable) {
button1.setBackgroundDrawable(backgroundDrawable);
}else if (backgroundDrawable instanceof ColorDrawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
final ColorDrawable colorDrawable = (ColorDrawable) backgroundDrawable;
button1.setBackgroundColor(colorDrawable.getColor());
}
}
希望它会有所帮助。