现在的位置: 首页 > 综合 > 正文

背景选择器之代码实现

2018年08月27日 ⁄ 综合 ⁄ 共 682字 ⁄ 字号 评论关闭
        Drawable normal = null;
        Drawable pressed = null;
        Drawable focus = null;

        StateListDrawable stateListDrawable = new StateListDrawable();
        // 注意该处的顺序,只要有一个状态与之相配,背景就会被换掉
        // 所以不要把大范围放在前面了,如果sd.addState(new[]{},normal)放在第一个的话,就没有什么效果了
        stateListDrawable.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focus);
        stateListDrawable.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);
        stateListDrawable.addState(new int[] { android.R.attr.state_focused }, focus);
        stateListDrawable.addState(new int[] { android.R.attr.state_pressed }, pressed);
        stateListDrawable.addState(new int[] { android.R.attr.state_enabled }, normal);
        stateListDrawable.addState(new int[] {}, normal);

抱歉!评论已关闭.