어플리케이션, 앱 (Application)/안드로이드 (Android)

안드로이드 스튜디오 XML - 버튼 background drawable 적용이 안되는 이유

sobal 2025. 3. 9. 02:30

가장 쉬운 해결법부터 말하자면 버튼의 종류를 바꾸면 된다.


 

 

기본적으로 xml에서 버튼 background drawable 적용이 안되는 이유는 res/values/themes/themes.xml 에 있다.

 

 

버튼에 커스텀 배경을 입히려면, 버튼 스타일을 별도로 정의하고 테마에서 buttonStyle 혹은 materialButtonStyle에 연결해 주어야 안정적으로 적용할 수 있다.

 

<!-- styles.xml -->
<resources>
    <!-- 커스텀 버튼 스타일 -->
    <style name="MyCustomButtonStyle" parent="@style/Widget.MaterialComponents.Button">
        <item name="android:background">@drawable/my_button_background</item>
        <!-- 추가로 필요한 속성들 -->
    </style>
</resources>

<!-- 테마에서의 적용 -->

<!-- themes.xml -->
<resources>
    <style name="Theme.AlertFlash" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- 버튼 스타일 오버라이드 -->
        <item name="materialButtonStyle">@style/MyCustomButtonStyle</item>
    </style>
</resources>

<!-- 레이아웃에서의 적용 -->

<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/MyCustomButtonStyle"
    ... />


만약 AppCompat 라이브러리만 사용하고 있고, Material Components는 쓰지 않는다면, 부모 테마를 Theme.AppCompat.Light.NoActionBar 등으로 변경하고 버튼 스타일을 오버라이드하면 된다.

<!-- themes.xml -->
<resources>
    <style name="Theme.AlertFlash" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:buttonStyle">@style/MyAppCompatButtonStyle</item>
    </style>
</resources>

<resources>
    <style name="MyAppCompatButtonStyle" parent="@style/Widget.AppCompat.Button">
        <item name="android:background">@drawable/my_button_background</item>
    </style>
</resources>


아니면 처음 말했던 것처럼 버튼 자체를 androidx.appcompat.widget.AppCompatButton 로 쓰면 바로 해결이 될거다.