2

我想創建一個帶彩色正方形的選項組,供用戶選擇一個。它正在開發一種3.2設備上,並且在這張圖片看起來像: enter image description hereStateListDrawable與編程創建的LayerDrawable不能在Android 4.0+上工作

的代碼是一樣的東西:

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設備上的所有方塊是黑色的,檢查時不改變: enter image description here

這個問題似乎加入可繪製到StateListDrawable時.. 任何想法如何解決這個問題?

回答

1

我解決了黑色方塊問題,通過移除擴展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 }); 
... 
+0

爲什麼它使用StateListDreawable工作? – jonney 2014-10-24 22:15:29

0

在我的模擬器與android 4.2修訂版2(最新更新),複選框不會出現,我發現問題是:當setButtonDrawable爲null或ColorDrawable時,複選框度量它的大小爲0的寬度。這不是StateListDrawable的原因。

我嘗試設置複選框的寬度和高度,並且一切似乎都正常。 試試這個:box.setWidth(30); box.setHeight(30); 或者當您使用layoutparams(例如「new RelativeLayout.LayoutParams(50,50));」將複選框添加到佈局時。 希望這個幫助

+0

是的,我的複選框添加到使用的代碼波紋管中的LinearLayout,但廣場仍是全黑:LinearLayout.LayoutParams params1 =新LinearLayout.LayoutParams( 環境。 getResource()。getDimensionPixelSize( R.dimen.dashboard_item_size),context .getResources()。getDimensionPixelSize( R.dimen.dashboard_item_size)); – 2013-04-30 16:36:15

相關問題