2017-04-14 9 views
5

프로그래밍 방식으로 버튼의 drawableLeft/drawableRight 색상을 변경하는 방법을 알아 내려고하고 있습니다. 어떤 작품 아래에 언급 한 바와 같이 나는> 내 XML로 그릴 수 색조를 사용하고 23프로그래밍 방식으로 안드로이드에서 프로그래밍 방식으로 API 레벨 23 아래의 드로잉 색조를 변경하는 방법

<Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="VIEW ALL" 
       android:layout_centerInParent="true" 
       android:background="#00000000" 
       android:drawableLeft="@mipmap/ic_menu_black_36dp" 
       android:layout_centerVertical="true" 
       android:id="@+id/view_all" 
       android:textColor="@color/bottom_color" 
       android:drawableTint="@color/bottom_color" 
       /> 
     Button prev = (Button) findViewById(R.id.prev); 

    Drawable[] drawables =prev.getCompoundDrawables(); 
     drawables[0].setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY); 
     prev.setCompoundDrawables(drawables[0],null,null,null); 

솔루션 색상 < API 레벨을 변경할 수 API 레벨 23가 아니라 : 그래서 당신은 PorterDuff.Mode.MULTIPLY를 사용하는

Drawable[] drawablesprev =prev.getCompoundDrawables(); 

//for drawableleft drawable array index 0 

    drawablesprev[0].setColorFilter(getResources().getColor(R.color.assessment_bottom), PorterDuff.Mode.SRC_ATOP); 

//for drawableright drawable array index 2 
drawablesprev[2].setColorFilter(getResources().getColor(R.color.assessment_bottom), PorterDuff.Mode.SRC_ATOP); 


//for drawabletop drawable array index 1 
    drawablesprev[1].setColorFilter(getResources().getColor(R.color.assessment_bottom), PorterDuff.Mode.SRC_ATOP); 
+0

'android.support.v4.graphics.drawable.DrawableCompat'와'setTint (Drawable 드로어 블, int tint)'메소드 – pskink

+0

을 사용하면 동일한 샘플 코드를 제공 할 수 있습니다. 나는 여기에 붙어있는 찐다 –

+0

무엇에 갇혔습니까? 당신의 코드는 무엇입니까? – pskink

답변

0

, 당신은 색상을 곱합니다. 당신의 드로어 블 (Drawable)의 이름을 가정하면 아이콘은 검은 색이됩니다 - #000000 또는 int0이 될 것입니다. 다음 0 * GRAY (또는 다른 색상)은 항상

는, 예를 들어, 다른 PorterDuff.Mode의 시도 당신에게 여전히 0, 그래서 블랙을 ... 줄 것이다 PorterDuff.Mode.SRC_ATOP 또는 PorterDuff.Mode.SRC_IN

아마 여기 MULTIPLY

+0

getCompoundDrawables를 사용하여 작업했습니다. –

0

제대로 착색한다 아이콘의 흰색 버전에서 작동 현재 코드는 텍스트 뷰 또는 버튼 당김의 착색하는 빠른 방법입니다 :

private void tintViewDrawable(TextView view) { 
     Drawable[] drawables = view.getCompoundDrawables(); 
     for (Drawable drawable : drawables) { 
      if (drawable != null) { 
       drawable.setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP); 
      } 
     } 
    }