2013-08-28 1 views
2

드로어 블 배경을 목록 어댑터의 텍스트 뷰에 적용하려고합니다. 나는 드로어 블 배경 내가이프로그래밍 가능한 드로어 블 리소스의 배경색을 어떻게 변경합니까?

Drawable mDrawable = mContext.getResources().getDrawable(R.drawable.back); 

처럼 내 활동이 당김 요소를 얻을

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
    <solid android:color="@color/black" /> 
    <stroke android:width="1dip" android:color="#ffffff"/> 
</shape> 

XML로 정의하고 지금은 내가 너무 배경을 변경할 진수 코드의 다양한 문자열을 그러나 그것을하는 방법을 모릅니다. 색상 필터 또는 뭔가?

답변

2

한 가지 방법은 이것이다 :

public class MyDrawable extends ShapeDrawable{ 

      private Paint mFillPaint; 
      private Paint mStrokePaint; 
      private int mColor; 

      @Override 
      protected void onDraw(Shape shape, Canvas canvas, Paint paint) { 
       shape.drawPaint(mFillPaint, canvas); 
       shape.drawPaint(mStrokePaint, canvas); 
       super.onDraw(shape, canvas, paint); 
      } 

      public MyDrawable() { 
       super(); 
       // TODO Auto-generated constructor stub 
      } 
      public void setColors(Paint.Style style, int c){ 
       mColor = c; 
       if(style.equals(Paint.Style.FILL)){ 
        mFillPaint.setColor(mColor);      
       }else if(style.equals(Paint.Style.STROKE)){ 
        mStrokePaint.setColor(mColor); 
       }else{ 
        mFillPaint.setColor(mColor); 
        mStrokePaint.setColor(mColor); 
       } 
       super.invalidateSelf(); 
      } 
      public MyDrawable(Shape s, int strokeWidth) { 
       super(s); 
        mFillPaint = this.getPaint(); 
        mStrokePaint = new Paint(mFillPaint); 
        mStrokePaint.setStyle(Paint.Style.STROKE); 
        mStrokePaint.setStrokeWidth(strokeWidth); 
        mColor = Color.BLACK; 
      } 

     } 

사용법 :

MyDrawable shapeDrawable = new MyDrawable(new RectShape(), 12); 
//whenever you want to change the color 
shapeDrawable.setColors(Style.FILL, Color.BLUE); 

또는 시도, 두 번째 방법, ShapeDrawable에 드로어 블 캐스팅, Paint를 별도 작성 등으로 설정할 : castedShapeDrawable.getPaint().set(myNewPaint);

관련 문제