首页 > 其他 > 详细

StateListDrawable

时间:2016-10-01 17:22:57      阅读:232      评论:0      收藏:0      [点我收藏+]
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

原文:http://www.cnblogs.com/ywq-come/p/5925982.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!