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