StateListDrawable states = new StateListDrawable();int yourBackgroundColor = Color.parseColor("#FFFFFF");// Add specific color when your view has state 'pressed'states.addState(new int[] {android.R.attr.state_pressed}, new ColorDrawable(yourBackgroundColor));// Add other states wanted and associated drawable// ...// As StateListDrawable extend Drawable, you can use it as background for exemple yourView.setBackground(states);您可以在StateListDrawable中添加任意数量的状态(可用状态列表:http : //developer.android.com/guide/topics/resources/color-
list-resource.html)。对于每种状态组合,您可以设置特定的和动态的可绘制对象。
您可以指定多个状态以匹配可绘制对象
states.addState(new int[] { -android.R.attr.state_focused, android.R.attr.state_selected, -android.R.attr.state_pressed}, ColorDrawable(yourBackgroundColor));这次,如果您的视图未聚焦,未选中且未按下,则将应用颜色。



