2016-09-21 6 views
0

내 체크 박스의 배경이 drawable xml을 통해 지정되었으며 코드를 통해이 항목의 색상을 변경하고 싶습니다. 안드로이드에서 다른 컨트롤은 색상을 설정하는 다른 방법이있는 것 같습니다. 드로어 블/사진 custom_checkbox.xml에서xml에 지정된 체크 박스 드로어 블의 색상을 변경하는 방법은 무엇입니까?

<Checkbox android:button="@drawable/custom_checkbox" /> 

:에

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="true" android:drawable="@drawable/checked" /> 
    <item android:state_checked="false" android:drawable="@drawable/unchecked" /> 
</selector> 

대부분의 답변은 SO 위의 솔루션에서 중지합니다. 다음 코드를 통해 색상을 변경할 수 있지만이 코드는 특정 API 레벨 (예 : 레벨 17)에서 작동하지 않습니다. 나는 전반적으로 효과가있는 것을 원합니다.

Drawable d = DrawableCompat.wrap(checkbox.getBackground()); 
DrawableCompat.setTint(d, newColor); 
+0

가능한 중복에 http : // 유래. com/questions/39475790/android-tablayout-icon-color-doesnt-change-when-dragging) –

+0

중복 된 것 같지 않습니다. 확인란이 아닌 탭에 대해 묻습니다. 솔루션에는 예제에 레이어 목록도 있는데, 그렇지 않습니다. – Boon

+0

예. 이 스레드에서 허용되는 대답은 모든 API 수준에서 드로어 블 상태를 채색하는 방법을 보여줍니다. –

답변

0

this이 도움이됩니다.

와 나는이 생각 : android:buttonTint="@color/mybrown"는 간단한 방법

+0

이 솔루션은 API 레벨 17 (또는 API 레벨 21 이하의 모든 항목)에서 작동하지 않습니다. – Boon

0

입니다 이것을 시도 : ([드래그 할 때 안드로이드, TabLayout 아이콘의 색상이 변경되지 않습니다]의

Drawable d = DrawableCompat.wrap(checkbox.getBackground()); 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     DrawableCompat.setTint(d, newColor); 

    } else { 
     d.mutate().setColorFilter(newColor, PorterDuff.Mode.SRC_IN); 
    } 
관련 문제