ColorFilter를 TextView의 CompoundDrawable에 걸어주세요.

3285 단어 TILAndroid

개요


안드로이드의 TextView에서 설정할 수 있는CompoundDrawable에 ColorFilter를 설치하는 방법에 대해 설명했습니다.

CompoundDrawable


TextView는 텍스트와 인접한 이미지, 즉 CommundDrawable을 설정할 수 있습니다.
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/ic_drawable_white"
    />
이것을 사용하면 ImageView와 TextView를 조합할 필요가 없기 때문에 간단하게 레이아웃 구성을 할 수 있습니다.표시되는 위치는 문자의 위아래 좌우입니다.위의 예는 drawable Left이기 때문에 텍스트의 왼쪽에 Drawable이 표시됩니다.

ColorFilter 중단 동기


내가 만든 프로그램에서 사용자는 프로그램 내 View 색상을 자유롭게 설정할 수 있습니다
ImageView를 사용하는 곳에는 흰색 Drawable이 준비되어 있으며 ColorFilter 방법을 사용하여 사용자의 색상 설정을 반영합니다.
하지만 ColorFilter를 어떻게 치는지 몰라서 Drawable의 색깔은 항상 흰색입니다.

이루어지다


설치 방법


조사해 보니 곧 방법을 찾았다.
Setting the Color of a TextView Drawable for Android

이루어지다


보도의 인코딩이 그렇게 예술적이지 않기 때문에 다른 작법을 시도해 보자.Kotlin이면 짧게 쓸 수 있어요.
textView.compoundDrawables?.forEach {
    it?.colorFilter = PorterDuffColorFilter(fontColor, PorterDuff.Mode.SRC_IN)
}

설치 후



보시다시피 ColorFilter는CompoundDrawable에 걸려 있습니다.

보태다


ViewStub 내 TextView에 좋지 않기 때문에 사용하는 것이 좋습니다TextViewCompat.setCompoundDrawableTintList.
textView?.also {
    it.setTextColor(fontColor)
    TextViewCompat.setCompoundDrawableTintList(it, ColorStateList.valueOf(fontColor))
}

총결산


Commund Drawarble에 ColorFilter를 걸려면 TextView의compound Drawables에서 각각의 Drawable 대상을 실행할 수 있습니다.TextView를 상속받은 버튼 등도 동일하게 적용된다.

좋은 웹페이지 즐겨찾기