android set button background programmatically

前端 未结 8 746
天涯浪人
天涯浪人 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条回答
  •  我在风中等你
    2020-11-30 03:46

    Old thread, but learned something new, hope this might help someone.

    If you want to change the background color but retain other styles, then below might help.

    button.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
    

提交回复
热议问题