Android Button Styling Programmatically

前端 未结 3 1918
死守一世寂寞
死守一世寂寞 2020-12-15 13:25

How do you programmatically add/remove style to an android button? Is it possible to apply the styling at runtime?

I have two buttons that look like these

              


        
3条回答
  •  無奈伤痛
    2020-12-15 14:15

    You can't apply xml-defined styles in runtime (from code). If you want to change background and font style when button is clicked (pressed) you should create selector which defines what background to use for normal button or for clicked state.

    If selector is not what you want, you should manually set every button property to desired value via button's setXXX method of Button class.

    P.S. You can swap old button for a new another one inflated from xml with different style. But this is not a good way I suppose...

提交回复
热议问题