티스토리 뷰

반응형

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))
반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함