How to change the color of a button?

后端 未结 8 1821
臣服心动
臣服心动 2021-02-03 20:08

I\'m new to android programming. How do I change the color of a button?

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-03 20:32

    You can change the value in the XML like this:

    Here, you can add any other color, from the resources or hex.

    Similarly, you can also change these values form the code like this:

    demoButton.setBackgroundColor(Color.WHITE);
    

    Another easy way is to make a drawable, customize the corners and shape according to your preference and set the background color and stroke of the drawable. For eg.

    button_background.xml

    
    
        
        
        
    
    

    And then set this shape as the background of your button.

    Hope this helps, good luck!

提交回复
热议问题