Android ColorStateList可以添加xml文件可以结合selector使背景颜色变化更加自由。
selector的基本含义和用法可以参照我的另一篇博客,
两种方式进行调用:
1.Button btn=(Button)findViewById(R.id.btn);
Resources resource=(Resources)getBaseContext().getResources();
ColorStateList csl=(ColorStateList)resource.getColorStateList(R.color.button_text);
if(csl!=null){
btn.setTextColor(color_state_list);//设置按钮文字颜色
}
2.
XmlResourceParser xpp=Resources.getSystem().getXml(R.color.button_text);
try {
ColorStateList csl= ColorStateList.createFromXml(getResources(),xpp);
btn.setTextColor(csl);
} catch (Exception e) {
// TODO: handle exception
}
就可以实现button或者text的按下或者不按下颜色的更换。
若有问题请留言,若转载注明出处。