Android

[Android] Material Theme 사용 시 Button 레이아웃

HAINIX_X 2020. 10. 7. 11:00
반응형

안드로이드에서 여러가지 이유로 style에 테마를 MaterialComponents 로 사용하는 경우가 많다.

하지만 적용 후 일반 Button의 레이아웃이 먹지를 않거나 백그라운드 및 다크테마가 적용이 되는 경우가 많은데 나도 이런 상황 때문에 안드로이드 버튼에 시간을 꽤 써서 같은 실수를 반복하지 않기 위해 적어둔다.

 

기존 레이아웃.xml

<Button
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:background="@drawable/button_round_main_color"
	android:gravity="center"
	android:stateListAnimator="@null"
	android:textColor="@color/mainColorText"
	android:textStyle="bold" />

 

 

바뀐 레이아웃.xml

<androidx.appcompat.widget.AppCompatButton
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:background="@drawable/button_round_main_color"
	android:gravity="center"
	android:stateListAnimator="@null"
	android:textColor="@color/mainColorText"
	android:textStyle="bold" />

 

이렇게 Button > androidx.appcompat.widget.AppCompatbutton 이라고 명시해 주면 MaterialButton과 헷갈리지 않아 레이아웃이 적용이 되는 걸 볼 수 있다. 이거 때문에 거의 하루 날렸다...

 

 

 

참고한 링크 : 

https://stackoverflow.com/a/52673168/7017299

 

Can't use android:background with button from the new material components

I'm using the new material components com.google.android.material:material with android x but I can't set a custom background to the button. I know that I can use app:backgroundTint to change the ...

stackoverflow.com

 

반응형