How to change the color of a button?

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

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

8条回答
  •  广开言路
    2021-02-03 20:48

    The RIGHT way...

    The following methods actually work.

    if you wish - using a theme
    By default a buttons color is android:colorAccent. So, if you create a style like this...

    
    

    You can use it like this...

    alternatively - using a tint
    You can simply add android:backgroundTint for API Level 21 and higher, or app:backgroundTint for API Level 7 and higher.

    For more information, see this blog.

    The problem with the accepted answer...

    If you replace the background with a color you will loose the effect of the button, and the color will be applied to the entire area of the button. It will not respect the padding, shadow, and corner radius.

提交回复
热议问题