【发布时间】:2016-07-12 22:45:45
【问题描述】:
我只是想通过文本的值来改变按钮的背景颜色。例如,当我将文本更改为:
button.setText("YES");
我想将按钮的背景颜色设置为绿色。 当我将文本更改为:
button.setText("NO");
我想将按钮的背景颜色设置为红色。
当我像这样在 java 代码中更改它时:
boolean textValueYES = true;
button.setBackgroundColor(textValueYES ? Color.GREEN : Color.RED);
按钮失去其 drawable.xml 设置。 有没有办法将此检查添加到可绘制的 xml 中? 或者通过其文本值设置背景颜色而不丢失可绘制设置?
【问题讨论】:
-
将按钮背景设置为可绘制使用
button.setBackgroundResource(textValueYES ? R.drawable.greendrawable : R.drawable.reddrawable); -
创建具有红色背景颜色和绿色背景颜色的可绘制文件,例如:red_drawable.xml,green_drawable.xml,并使用button.setBackgroundResource(R.drawable.red_drawable);
标签: android css colors background drawable