본문 바로가기

Android/Kotlin

[Kotlin]Android Button의 background 적용 안됨 drawable 설정법(style , theme)

 

android studio에는 기본 style들을 모아 놓은 theme가 있다.

새로운 프로젝트를 생성한 후 버튼을 만들어보면 다음과 같이 보라색 배경에 흰색 text를 갖고있는 button이 생성된다.

이같은 설정은 res - values - themes 에서 확인해 볼 수 있다.

그리고 현재 적용되어있는 테마는 AndroidManifest.xml 의 application 에서 적혀있다.

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.Buttontest"
    tools:targetApi="31">
    <activity

 

바로 이것때문에 layout에서 button의 background에 색을 변경하던 이미지를 변경하던 보라색 버튼으로 나온다.

해결법으론 2가지가 있다.

 

첫번째는 기본 설정된 theme를 변경하는 것이다.

parent = "Theme.MateriallComponents.DayNight.DarkActionBar (변경전)

parent = "Theme.AppCompat.DayNight.DarkActionBar (변경후)

이와 같이 변경하면 색이 바뀌는 것을 알수 있다.

 

두번째 방법은 layout에서 button을 생성할 때

<button.../> 대신

<androidx.appcompat.widget.AppCompatButton .. /> 을 통하여 버튼을 생성하면 background를 변경할 수 있다.

 

색을 변경한 layout.. 

보라색이 사라졌다!

마찬가지로 drawable에 이미지 파일을 백그라운드로 넣어도 바뀌는 모습을 확인할 수 있었다.

 

추가적으로 다음 코드를 theme에 추가함으로 앱 전체 font를 바꾸고 actionbar와 상태창을 숨길 수 있다.

1
2
3
4
5
6
7
8
9
10
11
 
        //font를 추가한다.
        <item name="fontFamily">@font/newfont</item>
        <item name="android:fontFamily">@font/newfont</item>
        
        //ActionBar삭제
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        
        //풀스크린으로 만들기 (상태바 숨기기)
        <item name="android:windowFullscreen">true</item>
cs