개요
라운드된 버튼을 만들기 위해 drawable을 만들어 background에 사용하려고 했었다.
drawable
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/green"/>
<corners android:radius="36dp" />
</shape>
레이아웃
1
2
3
4
5
6
7
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/round_green_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
그런데 drawable에 적용한 solid 색상이 적용되지 않는 문제가 있었다.
난 분명 초록색으로 설정했었는데…
원인은 알 수 없지만..
정확한 원인은 정확히 알 수 없으나.. 일반 Button
이 MaterialButton
의 디자인을 이용하고 있어서가 아닐까라는 추측이 들었다. 그 이유는 com.google.android.material.button.MaterialButton
으로 바꿔서 테스트해봐도 동일한 문제가 있었고, 찾아본 결과 아래 내용을 확인할 수 있었다.
요약하자면, MaterialButton
에서는 android:background
를 사용하지 말라는 뜻인데, 그 이유가 MaterialButton
자체에서 자체 background drawable
을 소유하고 있고, 이것을 강제로 변경하게 되면 문제가 생길 수 있다는 뜻이다.
해결방법
인터넷에 나와있는 해결 방법은 여러가지이다.
themes.xml
에서 최상단 스타일에DarkActionBar.Bridge
로 변경해준다.- 버튼 xml에서
app:backgroundTint="@null"
로 설정한다. primaryColor
를 변경해준다.Button
이나MaterialButton
대신AppCompatButton
을 이용한다.
여기서 제일 깔끔하다고 생각하는건 차라리 AppCompatButton
을 이용하는 것이다. 그 이유는 1번같은 경우 theme을 변경하는 것이기 때문에 프로젝트 전체가 변경될 위험성이 있다. 2번째는 매번 버튼을 만들고 custom drawable을 적용할 때마다 써줘야되는게 불합리적이라고 판단했다. 3번째는 primaryColor
를 변경하면 이 역시 프로젝트 전체 디자인이 변경될 가능성이 있다. 그래서 4번을 선택했다.
[참고]
- https://developer.android.com/reference/com/google/android/material/button/MaterialButton
- https://github.com/material-components/material-components-android/issues/889
Comments powered by Disqus.