Android

Android] Button 배경 색 설정 시 변경 안되는 경우

김코식 2022. 1. 16. 17:39

안드로이드 스튜디오에서 버튼을 생성했을 때 배경이 변하지 않을 때

 

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:text="Button"
    android:background="#ffff00"
    tools:layout_editor_absoluteX="70dp"
    tools:layout_editor_absoluteY="60dp" />

 

다음과 같이 배경색을 #ffff00(노란색) 으로 지정했지만 

배경 색이 변하지 않았을 때

 

1. res - values - themes 클릭

2. 클릭시 나오는 코드

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.Round_button" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- 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>

이 코드 중 

<style name="Theme.Round_button" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

이 부분의 parent 내용을 

"Theme.AppCompat.Light" 으로 작성.

 

설정대로 버튼의 배경 색이 설정되는 것을 볼 수 있다.