Android關于Button背景或樣式失效問題解決方法
前言
最近在學習安卓開發(fā)的時候遇到了一個問題,使用Android Studio在為Button設置背景顏色的時候發(fā)現設置好后卻在運行模擬機上失效了。經過一番查閱資料后才有了正確的解決辦法,相信這是很多初學Android開發(fā)的朋友都會遇到的一個問題,希望此篇對大家有所幫助。
問題描述:
使用Android Studio進行安卓開發(fā)時Button的背景色一直無法修改,呈現亮紫色(呈現顏色額和主題有關,我的是亮紫色)。
以其中一個Button舉例,代碼是這樣的:
<Button android:id="@+id/btn_1" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="按鈕1" android:textSize="20sp" android:textColor="#0066FF" android:backgroundTint="@null" android:background="#FF0000"/>
正常運行的話第一個Button應該是紅色的,但是在模擬機上確實這樣:
問題原因:
出現該問題的原因主要是因為使用Android Studio 4.1之后的版本進行開發(fā)時,創(chuàng)建的項目默認的主題都是Theme.MaterialComponents.DayNight.DarkActionBar
。所有Button都是Material類型的Button,默認使用主題色。
解決方法:
在左側project欄中找到app/src/main/res/values/themes.xml
將其中的
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
修改為:
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
或Theme.AppCompat下的任意一種主題:
<style name="Theme.MyApplication" parent="Theme.AppCompat.Light">
解決后運行結果:
這時候我們會發(fā)現問題已經被完美的解決啦~
后來又學到了一種更為簡單的方法,在使用Button時用android.widget.Button代替Button就可以不用那么麻煩的改設置啦,這無疑是一種更好的方法:
將
<Button android:id="@+id/btn_1"
改為:
<android.widget.Button android:id="@+id/btn_1"
即可
總結
到此這篇關于Android關于Button背景或樣式失效問題解決方法的文章就介紹到這了,更多相關Android Button背景或樣式失效內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android RecyclerView實現數據列表展示效果
這篇文章主要為大家詳細介紹了Android RecyclerView實現數據列表展示效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07