我正在尝试创建一个带有彩色正方形的选项组,以供用户选择。它正在3.2设备上运行,如下图所示:


代码类似于:

for (int i = 0; i < COLORS.length; i++) {
        CheckBox box = new CheckBox(context);
        box.setBackgroundDrawable(getColorOption(context, COLORS[i]));
        box.setButtonDrawable(android.R.color.transparent);


然后,在getColorOption函数中,创建StateListDrawable:

    StateListDrawable slDrawable = new StateListDrawable();

    LayerDrawable checkedDrawable = new LayerDrawable(new Drawable[] {
            new SelectFrameShapeDrawable(transparentColor, lightRedColor),
            new SquareShapeDrawable(color) });
    LayerDrawable uncheckedDrawable = new LayerDrawable(new Drawable[] {
                    new SelectFrameShapeDrawable(transparentColor, transparentColor),
                    new SquareShapeDrawable(color) });
    slDrawable.addState(new int[] { android.R.attr.state_checked },
            checkedDrawable);
    slDrawable.addState(new int[] { -android.R.attr.state_checked }, uncheckedDrawable);
    return slDrawable;


SquareShapeDrawable类是ShapeDrawable:

public class SquareShapeDrawable extends ShapeDrawable {
    private final Paint fillpaint;

    public SquareShapeDrawable(int color) {
        super(new RectShape());
        fillpaint = new Paint(this.getPaint());
        fillpaint.setColor(color);
    }

    @Override
    protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
        shape.draw(canvas, fillpaint);
    }
}


而SelectFrameShapeDrawable是:

private class SelectFrameShapeDrawable extends ShapeDrawable {
private final Paint fillpaint, strokepaint;

public SelectFrameShapeDrawable(int fill, int stroke) {
        super(new RectShape());
        strokepaint = new Paint(this.getPaint());
        strokepaint.setStyle(Paint.Style.STROKE);

        strokepaint.setStrokeWidth((int) (getResources()
                .getDisplayMetrics().density + 0.5f));
        strokepaint.setColor(stroke);
        int padding = (int) (4 * getResources().getDisplayMetrics().density + 0.5f);
        setPadding(padding, padding, padding, padding);

        fillpaint = new Paint(strokepaint);
        fillpaint.setColor(fill);
    }

    @Override
    protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
        if (strokepaint != null)
            shape.draw(canvas, strokepaint);
        shape.draw(canvas, fillpaint);
    }
}


在4.2设备上,所有正方形均为黑色,并且选中后不会更改:


问题似乎出在将可绘制对象添加到StateListDrawable时。
任何想法如何解决这个问题?

最佳答案

我通过删除扩展了ShapeDrawable的自定义类并用直接使用ShapeDrawable类的代码库代替了它们来解决了黑色正方形问题。该代码似乎可以在所有平台上运行。

奇怪的是,原始问题出现在4.2而不是3.2上。我最初的灵感来源是:http://www.betaful.com/2012/01/programmatic-shapes-in-android/

    ShapeDrawable selectFrame = new ShapeDrawable();
    selectFrame.setShape(new RectShape());
    selectFrame.getPaint().setColor(lightRedColor);
    selectFrame.getPaint().setStyle(Paint.Style.STROKE);
    selectFrame.getPaint().setStrokeWidth((int) (getResources().getDisplayMetrics().density + 0.5f));
    int padding = (int) (4 * getResources().getDisplayMetrics().density + 0.5f);
    selectFrame.setPadding(padding, padding, padding, padding);

    ShapeDrawable square = new ShapeDrawable();
    square.setShape(new RectShape());
    square.getPaint().setColor(color);

    LayerDrawable checkedDrawable = new LayerDrawable(new Drawable[] {
            selectFrame, square });
...

10-08 09:12