Android Studio - 在按钮上设置 RGB

Android Studio - Set RGB on a button

我在 Android 中使用 Android Studio 进行编程,我需要在按钮上设置 RGB 比例的颜色(不是在 xml 中,而是在 java代码)。我该怎么办?

Button button = (Button)findViewById(R.id.YOUR_BUTTON_ID); 

button.setBackground(R.color.YOUR_COLOR_RESOURCE);

只需将您的 rgb 代码放入此网站 http://www.rgbtohex.net/ 并使用十六进制代码(以 # 开头的代码)使用:

button.setBackgroundColor(Color.parseColor(hexcodefromwebsite));