問題簡述:
在修改Button的背景顏色時,始終無法修改顏色為設定的顏色,且顏色始終為默認的藍紫色,
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_1"
android:backgroundTint="@null"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:background="#FF0011"></Button>
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_2"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:backgroundTint="@null"
android:background="@drawable/bg_btn2"
android:layout_below="@id/btn_1"
android:layout_marginTop="20dp"></Button>
<Button
android:id="@+id/btn_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕3"
android:textSize="20sp"
android:backgroundTint="@null"
android:textColor="#FF9900"
android:background="@drawable/bg_btn3"
android:layout_below="@id/btn_2"
android:layout_marginTop="20dp"></Button>
<Button
android:id="@+id/btn_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕4"
android:textSize="20sp"
android:backgroundTint="@null"
android:textColor="#FFFFFF"
android:background="@drawable/bg_btn4"
android:layout_below="@id/btn_3"
android:layout_marginTop="20dp"></Button>
上文中drawable/bg_btn等顏色為非默認藍紫色,
實際模擬機效果(真機效果一樣)如下:

問題解決:
博主的解決方法:
默認的顏色設定來自于res/values/themes.xml與夜間模式(應該是)下的res/values-night/themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.HelloWorld" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
將(或你選擇的其它主題)
<style name="Theme.HelloWorld" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
修改為(或其它能夠實作非默認顏色的主題)
<style name="Theme.HelloWorld" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
即可解決問題,修改后的效果圖如下:

當然,經過搜索,還得到了以下解決方法,雖然沒有解決我的問題,但是希望能夠幫到你:
1、給按鈕增加一個屬性app:backgroundTint="@null"
2、點擊選單中的 “baiFile” -> “Invalidate Caches / Restart”,然后點擊對du話框中的 “Invalidate and Restart”,清空 cache 并且重啟,
3、更換舊版本(或許確實是版本問題)
4、將themes檔案下的<style> 中的所有默認屬性注釋掉(親測有效,或許這才是正確方法)
問題總結:
雖然不知道原理是什么,但是在搜索問題的時候很少人遇到這種問題,且遇到問題的多半是近期提問,總結來說應該是更新問題導致的,或者說因為我選擇了空白專案導致使用了默認的themes,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/225384.html
標籤:其他
上一篇:微信小程式分頁功能實作
下一篇:Banner輪播圖框架的基本用法
