android set button background programmatically

前端 未结 8 729
天涯浪人
天涯浪人 2020-11-30 02:57

I would like to know how to set the button color programatically? I have coded the following but fails:

Button11.setBackgroundColor(R.color.red);
         


        
8条回答
  •  -上瘾入骨i
    2020-11-30 03:45

    For not changing the size of button on setting the background color:

    button.getBackground().setColorFilter(button.getContext().getResources().getColor(R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
    

    this didn't change the size of the button and works with the old android versions too.

提交回复
热议问题