简体   繁体   English

以编程方式创建的带有LayerDrawable的StateListDrawable以编程方式无法在Android 4.0+上运行

[英]StateListDrawable with LayerDrawable created programatically not working on Android 4.0+

I am trying to create an option group with coloured squares for the user to choose one. 我正在尝试创建一个带有彩色正方形的选项组,以供用户选择。 It is working on a 3.2 device and looking like in this picture: 它正在3.2设备上运行,如下图所示: 在此处输入图片说明

The code is something like: 代码类似于:

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);

And then, in the getColorOption function I create the StateListDrawable: 然后,在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;

The SquareShapeDrawable class is a ShapeDrawable: 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);
    }
}

And the SelectFrameShapeDrawable is: 而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);
    }
}

On a 4.2 device all the squares are black and do not change when checked: 在4.2设备上,所有正方形均为黑色,并且选中后不会更改: 在此处输入图片说明

The problem seems to be when adding the drawables to the StateListDrawable... Any idea how to solve this? 问题似乎出在将可绘制对象添加到StateListDrawable时...任何想法如何解决此问题?

I solved the black squares issue by removing the custom classes that extended ShapeDrawable and replaced them with the code bellow that uses the ShapeDrawable class directly. 我通过删除扩展了ShapeDrawable的自定义类并将其替换为直接使用ShapeDrawable类的以下代码来解决了黑色正方形问题。 This code seems to work on all platforms. 该代码似乎可以在所有平台上运行。

It is weird though that the original issue was present on 4.2 and not on 3.2. 奇怪的是,原始问题出现在4.2而不是3.2上。 My original source of inspiration was this: http://www.betaful.com/2012/01/programmatic-shapes-in-android/ 我最初的灵感来源是: 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 });
...

In my simulator with android 4.2 revision 2 (the lastest update), the checkbox does not appear, and I found the problem is: when setButtonDrawable as null or ColorDrawable, the checkbox measure it's size is 0 of width. 在我的带有android 4.2修订版2(最新更新)的模拟器中,没有出现该复选框,而我发现问题是:当setButtonDrawable为null或ColorDrawable时,该复选框的大小为width的0。 This is not cause of the StateListDrawable. 这不是StateListDrawable的原因。

I try to set width, and height of checkbox, and everything seems OK. 我尝试设置复选框的宽度和高度,一切似乎都OK。 Try this: box.setWidth(30); 试试这个:box.setWidth(30); box.setHeight(30); box.setHeight(30); Or when you add the checkbox to the layout using the layoutparams like"new RelativeLayout.LayoutParams(50, 50));". 或者,当您使用诸如“ new RelativeLayout.LayoutParams(50,50));”之类的layoutparams将复选框添加到布局中时。 Hope this help 希望这个帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM