首页 > 代码库 > StateListDrawable
StateListDrawable
Integer[] mButtonState = { R.drawable.defaultbutton,
R.drawable.focusedpressed, R.drawable.pressed };
Button mButton = (Button) findViewById(R.id.button);
mButton.setBackgroundDrawable(myButton.setbg(mButtonState));
public StateListDrawable setbg(Integer[] mImageIds) {
StateListDrawable bg = new StateListDrawable();
Drawable normal = this.getResources().getDrawable(mImageIds[0]);
Drawable selected = this.getResources().getDrawable(mImageIds[1]);
Drawable pressed = this.getResources().getDrawable(mImageIds[2]);
bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);
bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);
bg.addState(View.ENABLED_STATE_SET, normal);
bg.addState(View.FOCUSED_STATE_SET, selected);
bg.addState(View.EMPTY_STATE_SET, normal);
return bg;
}
在输入法中有如下的使用
StateListDrawable mStateListDrawable = new StateListDrawable();
mStateListDrawable.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(mKeyBackgroundId));
mStateListDrawable.addState(new int[0], getResources().getDrawable(R.drawable.transparent));
StateListDrawable 可以通过如下的方法获取点击态的drawable:
StateListDrawable drawable = ((StateListDrawable)background);
drawable.setState(new int[]{android.R.attr.state_focused,android.R.attr.state_pressed});
来自为知笔记(Wiz)
StateListDrawable
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。