android 代码生成selector drawable

public static StateListDrawable makeSelector(Context context, int idNormal, int idPressed, int idFocused) {
StateListDrawable bg = new StateListDrawable();
Drawable normal = context.getResources().getDrawable(idNormal);
Drawable pressed = context.getResources().getDrawable(idPressed);
Drawable focused = context.getResources().getDrawable(idFocused);
bg.addState(new int[]{android.R.attr.state_pressed,}, pressed);
bg.addState(new int[]{android.R.attr.state_focused}, focused);
bg.addState(new int[]{}, normal);
return bg;
}

注意: 1.normal state要在最后add
2.使用button测试,ImageView 需要设置clickable为true
posted on 2015-10-20 10:36  songsyl1207  阅读(732)  评论(0编辑  收藏  举报