码迷,mamicode.com
首页 > 其他好文 > 详细

StateListDrawable

时间:2016-10-01 17:25:07      阅读:148      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!