방명록
- 안드로이드 프로그래밍 - 버튼 색상 변경2023년 10월 15일 21시 59분 16초에 업로드 된 글입니다.작성자: Moonsu99
환경
- OS - Mac OS 13.5.2
- Tools - Android Studio Iguana | 2023.2.1 Canary 5
- Language - Kotlin
- android version - 12
- tartgetSDK - 33
- minSDK - 28
요약
안드로이드 스튜디오로 앱을 개발 할 때, 버튼의 background속성을 주었음에도 불구하고 버튼색상이 변경되지 않는 경우가 있다.
이는 안드로이드 버전이 올라가며 생기는 이슈이며 여러 해결방안이 존재한다. 이번 포스트는 이를 다루는 방법을 설명하겠다.
해결방안
여러가지 방법으로 버튼 색상을 변경하는 방법을 설명하겠다.
1. Themes.xml수정
<resources xmlns:tools="http://schemas.android.com/tools"> <!-- Base application theme. --> <style name="Base.Theme.Applyserviceandroid" parent="Theme.Material3.DayNight.NoActionBar"> </resources>프로젝트 생성 시 다음과 같이 설정되어 있을텐데 여기서 테마를 Material3를 사용하지 않고 parent="Theme.AppCompat.dark~
등으로 변경하면 background color를 변경할 수 있다.
2. background 속성이 아닌 backgroundTint사용
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/black" android:text="this is button"/>버튼의 속성을 background가 아닌
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:backgroundTint="@color/black" android:text="this is button"/>backgroundTint로 작성을 한다면 정상적으로 색상이 변경되는 것을 확인할 수 있다.
3. button 선언 시 Button이 아닌 AppCompatButton 사용
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/black" android:text="this is button"/>이러한 버튼을 선언하는 것이 아닌
<androidx.appcompat.widget.AppCompatButton android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/black" android:text="this is button"/>AppCompatButton을 선언하여 background속성을 넣으면 정상적으로 색이 변경된다.
'Android [ Java, Kotlin ]' 카테고리의 다른 글
안드로이드 프로그래밍 - 인텐트(Intent)와 Finish (0) 2023.10.15 안드로이드 프로그래밍 - 타이틀바(Action bar) 제거 방안 (1) 2023.10.15 안드로이드 상태바(StatusBar) 설정 (0) 2023.10.14 안드로이드 프로그래밍 과제(Java) - TimePicker(타임 피커) (0) 2023.10.14 모바일 프로그래밍 과제(Java) - 중첩 레이아웃 (2) 2023.10.14 다음글이 없습니다.이전글이 없습니다.댓글