How to change the color of a button?

后端 未结 8 1819
臣服心动
臣服心动 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:49

    Through Programming:

    btn.setBackgroundColor(getResources().getColor(R.color.colorOffWhite));
    

    and your colors.xml must contain...

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
     <color name="colorOffWhite">#80ffffff</color>
    </resources>
    
    0 讨论(0)
  • 2021-02-03 20:50

    If you are trying to set the background as some other resource file in your drawable folder, say, a custom-button.xml, then try this:

    button_name.setBackgroundResource(R.drawable.custom_button_file_name);
    

    eg. Say, you have a custom-button.xml file. Then,

    button_name.setBackgroundResource(R.drawable.custom_button);
    

    Will set the button background as the custom-button.xml file.

    0 讨论(0)
提交回复
热议问题