chacha's

💣 Button에 Drawable 적용 안 되는 오류 본문

Android/TIL

💣 Button에 Drawable 적용 안 되는 오류

Cha_Cha 2021. 6. 14. 18:54
 Android Button background color not changing - stack overflow
 Difference between an AppCompat view and a normal Android viewAsk Question - stack overflow
 Can't change button's background color in XML .Android studio - stack overflow
 AppCompatButton - Documentation
 를 참고하여 작성하였습니다.

 

Button 사용했을 때

drawable를 사용하여 Button의 색상을 변경해줬는데 계속 적용이 되지 않는 문제가 발생하였습니다. stack overflow의 글을 살펴본 결과, style name="AppTheme"을 무엇을 사용하는지에 따라서 달라지는 문제인 것 같았습니다. parent로 Theme.MaterialComponents.DayNight.NoActionBar을 사용하고 있었는데 Theme.AppCompat.Light.NoActionBar을 사용하면 제대로 style 속성을 적용할 수 있는듯 했습니다. 

또 다른 방법으로는 parent를 바꾸지 않고 AppCompactButton을 사용하여 해결할 수 있습니다. 저는 이 방법을 이용하여 해결하였습니다.

    <Button
        android:id="@+id/sunday"
        style="@style/RepeatSettingWeekButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:text="@string/sunday"
        android:stateListAnimator="@null"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/saturday"
        app:layout_constraintTop_toTopOf="parent" />

🔻 위의 코드를 아래와 같이 바꿔서 해결 할 수 있었습니다.

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/sunday"
        style="@style/RepeatSettingWeekButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:text="@string/sunday"
        android:stateListAnimator="@null"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/saturday"
        app:layout_constraintTop_toTopOf="parent" />

androidx.appcompat.widget.AppCompatButton 사용했을 때

 

 AppCompat View

AppCompat View Component는 이전 버전의 플랫폼과 호환되는 기능을 지원합니다. 대부분의 AppCompatView와 일반 View의 차이점은 동적으로 background tint를 허용한다는 것입니다.

Button과 AppCompatButton의 차이점은 

1. ViewCompat의 메서드인 backgroundTint를 통해서 동적으로 button의 background 색상을 변경할 수 있습니다.

2. R.attr.backgroundTint와 R.attr.backgroundTintMode 를 사용하여 background 색상을 변경할 수 있습니다.

3. R.attr.fontFamily을 사용하여 fontFamily를 설저할 수 있습니다.

레이아웃에서 Button을 사용할 때 자동으로 사용되며 최상위 Acivity / Dialog는 appcompat에서 제공합니다. 사용자 지정 뷰를 작성할 때만이 클래스를 수동으로 사용해야 합니다.

 

추가적으로 아래의 코드는 xml 속성 중 Button 그림자를 없애는 속성입니다.

        android:stateListAnimator="@null"

 

Comments