본문 바로가기
안드로이드

[Android Studio] TextView Drawable 동적으로 수정하기 (코틀린)

by 개발_블로그 2023. 10. 12.
반응형

TextView 를 사용할 때 아래와 같이 TextView 와 함께 Drawable 을 사용할 때가 있다. 

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/size20"
    android:layout_marginTop="@dimen/size08"
    android:background="?attr/selectableItemBackground"
    android:drawablePadding="@dimen/size04"
    android:gravity="center"
    android:text="string"
    android:textColor="@color/white"
    app:drawableLeftCompat="@drawable/svg_icon"
    app:drawableTint="@color/white"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="parent" />

<TextView

 

이 때 사용한 Drawable 을 동적으로 바꿔줘야 할 때 아래와 같이 사용할 수 있다. 

 

tv.apply {
    setCompoundDrawablesWithIntrinsicBounds(
        R.drawable.svg_icon_2,   //left
        0, // top
        0, //right
        0  //bottom
    )
}

 

 

추가 팁 ! 색상을 바꿔줘야 할 때 

 

tv.compoundDrawables[0].setTint(getColor(R.color.red))
반응형