Android按钮设置文字变色

currentButton.setTextColor(R.color.white);
这种直接值颜色的ID进去,发现文字都变成黑色的了,所以需要使用setTextColor(ColorStateList colors) 这个方法,传入ColorStateList对象
 
ColorStateList whiteColor=getResources().getColorStateList(R.color.white);
currentButton.setTextColor(whiteColor);
这样文字就可以变颜色了
 
ColorStateList 对象可以在 XML 中定义,像 color 一样使用,它能根据它应用到的 View 对象的状态实时改变颜色。例如, Button 可以存在多种状态( pressed focused other ),如果使用 ColorStateList ,你就能为它的每个状态提供不同的颜色。

你可能感兴趣的:(android,移动开发,休闲,文字变色,按钮设置)