一孙乖、問題描述
在設(shè)置button背景顏色后橄浓,button背景仍然為藍(lán)色。如下所示:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HelloWorld"
android:textSize="50sp"
android:background="@color/purple_200"
/>
image.png
二晦嵌、問題原因 / 解決
在使用Android Studio 4.1+ 進(jìn)行開發(fā)時(shí)同辣,創(chuàng)建的項(xiàng)目默認(rèn)的主題是Theme.MaterialComponents.DayNight.DarkActionBar
。所有Button都是Material類型的Button惭载,默認(rèn)使用主題色旱函。
解決方法:
- 將主題切換為:Theme.AppCompat.*** , 不使用Material控件
<style name="Theme.ShapePractice" parent="Theme.AppCompat.DayNight.DarkActionBar">
- 設(shè)置
android:backgroundTint="@null"
著色設(shè)置為null后再進(jìn)行背景設(shè)置
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:backgroundTint="@null"
android:background="@color/purple_200"
android:text="HelloWorld"
android:textSize="50sp" />
參考
android - How to change the color of a button? - Stack Overflow
android - How to change the color of a button? - Stack Overflow