0

我想#000000为选中和#FFFFFF未选中状态创建一个可绘制的选择器。

如何以编程方式创建可绘制对象?

目前我正在这样做:

StateListDrawable states = new StateListDrawable();
ColorDrawable cdPress = new ColorDrawable(0xFF0000);
ColorDrawable cdUnPress = new ColorDrawable(0x0101DF);

states.addState(new int[] { android.R.attr.state_selected}, cdPress);
states.addState(new int[] {-android.R.attr.state_selected}, cdUnPress);

view.setBackgroundDrawable(states);
view.setSelected(isSelected);
4

1 回答 1

1

创建 stateListDrawable 并传递给视图

   StateListDrawable stateListDrawable=new StateListDrawable();
   stateListDrawable.addState(new  int[]{android.R.attr.state_pressed}getColorDrawable(Colorcode));
   stateListDrawable.addState(new int[]{android.R.attr.state_focused},getColorDrawable(Colorcode));
    ...
    mView.setBackground(stateListDrawable);
    ...
    }
    private static Drawable getColorDrawable(int colorCode) {
            return new ColorDrawable(colorCode);
    }
于 2014-07-02T07:07:24.443 回答