환경

* Android Studio 4.1.1

* SDK API 29 (Android 10)

 

레이아웃 xml 편집 시에 버튼의 백그라운드 색상 변경 및 커스텀 버튼 적용이 안되는 문제

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:background="#ff0000"
        android:background="@color/black"
        android:background="@drawable/button1"
        
    />

위 코드와 같이 버튼에 대한 3가지의 백그라운드 설정을 각각 해줘도

모두 아래와 같이 모두 보라색 배경으로만 결과가 나온다.

 

4.1.1 부터(?) 앱의 테마를 Theme.MaterialComponents 이녀석을 기본 Default로 사용하면서 발생하는 문제.

 


첫번째 방법

 

아주 간단한 해결방법은 스튜디오 상의 앱 테마를 Theme.AppCompat.Light 등으로 바꿔주면 해결

 

프로젝트의 app - res - values - themes - themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyApplication" parent="Theme.AppCompat.Light">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

코드 3번째 줄의 parent="이곳을 수정해주면 된다"

 


두번째 방법

 

themes.xml 을 수정하지 않고 안드로이드 매니패스트(AndroidManifest.xml)에서 테마를 변경.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


    </application>

</manifest>

코드 11번째 줄의 android:theme="@style/이곳을 수정해주면 된다.">

 

기본값으로 설정 되어 있는 Theme.MyApplication은

첫번째 방법의 themes.xml 코드 3번째 줄을 보듯이 themes.xml 의 테마를 정의한 것

즉, 문제의 Theme.MaterialComponents 이녀석인셈

 


세번째 방법

 

첫번째 방법, 두번째 방법 모두 싫고 xml 코드상에서 해결하고 싶다면

두가지 방법이 존재

 

1. backgroundTint 사용

            android:background="#00ff00"
            android:backgroundTint="#00ff00"

코드의 1번줄이 안먹는다면

2번줄의 backgroundTint를 사용하면 바로 적용이 된다.

하지만 xml로 정의한 커스텀 버튼( android:backgroundTint="@drawable/custom_button" ) 사용이 불가능

따라서 다음 2번째 방법을 사용

 

2. androidx.appcompat.widget.AppCompatButton

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:background="#ff0000"
        android:background="@color/black"
        android:background="@drawable/button1"
        
    />

기존 Button 대신 androidx.appcompat.widget.AppCompatButton 이코드를 써주면 사용 가능

하지만 기존의 Button만 써주면 되던 코드가 말도안되게 길어지는 단점이 존재

 


 

찾다 찾다 하도 모르겠어서 되도않는 영어로 스택오버플로우에 질문해서 얻어낸 결과

 

https://stackoverflow.com/questions/65477334/android-button-background-color-not-changing-on-xml

 

Android Button background color not changing on xml

Android Stidio 4.1.1 SDK 29 (Android 10) I trying to change button background color on xml, but it doesn't changed. here's my code stackoverflow.com

 

블로그 이미지

jokey12

모든 글은 내가 보려고 작성함 Contact : jsung9912@gmail.com

,