首页 > 代码库 > 在类里面写代码,代替xml文件

在类里面写代码,代替xml文件

    就是这个,以前还真没有做过,这不,这次就见识过了。然后希望给自己一份记忆,给你们一份快捷而已。。。

    /**
     * 代码中设置一般selector
     * 
     * @param context
     * @param idNormal
     * @param idSelected
     * @param idFocused
     * @param idUnable
     * @return
     */
    public static StateListDrawable newSelector(Context context,
            Drawable idNormal, Drawable idSelected, Drawable idFocused,
            Drawable idUnable) {

        StateListDrawable bg = new StateListDrawable();
        // View.PRESSED_ENABLED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_selected,
                android.R.attr.state_enabled }, idSelected);
        // View.ENABLED_FOCUSED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_enabled,
                android.R.attr.state_focused }, idFocused);
        // View.ENABLED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_enabled }, idNormal);
        // View.FOCUSED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_focused }, idFocused);
        // View.WINDOW_FOCUSED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_window_focused }, idUnable);
        // View.EMPTY_STATE_SET
        bg.addState(new int[] {}, idNormal);

        return bg;
    }

   

    /**
     * 代码中设置按钮selector
     * 
     * @param context
     * @param idNormal
     * @param idPressed
     * @param idFocused
     * @param idUnable
     * @return
     */
    public static StateListDrawable newBtnSelector(Context context,
            Drawable idNormal, Drawable idPressed, Drawable idFocused,
            Drawable idUnable) {

        StateListDrawable bg = new StateListDrawable();
        // View.PRESSED_ENABLED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_pressed,
                android.R.attr.state_enabled }, idPressed);
        // View.ENABLED_FOCUSED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_enabled,
                android.R.attr.state_focused }, idFocused);
        // View.ENABLED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_enabled }, idNormal);
        // View.FOCUSED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_focused }, idFocused);
        // View.WINDOW_FOCUSED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_window_focused }, idUnable);
        // View.EMPTY_STATE_SET
        bg.addState(new int[] {}, idNormal);

        return bg;
    }

 okay,呵呵,我觉得肯定还有更好的方法,所以,就献丑了!