StateListDrawable
Posted 杨伟乔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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});
以上是关于StateListDrawable的主要内容,如果未能解决你的问题,请参考以下文章