日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android關于Button背景或樣式失效問題解決方法_Android

作者:*木易先生* ? 更新時間: 2022-04-06 編程語言

前言

最近在學習安卓開發的時候遇到了一個問題,使用Android Studio在為Button設置背景顏色的時候發現設置好后卻在運行模擬機上失效了。經過一番查閱資料后才有了正確的解決辦法,相信這是很多初學Android開發的朋友都會遇到的一個問題,希望此篇對大家有所幫助。

問題描述:

使用Android Studio進行安卓開發時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之后的版本進行開發時,創建的項目默認的主題都是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">

解決后運行結果:

這時候我們會發現問題已經被完美的解決啦~

后來又學到了一種更為簡單的方法,在使用Button時用android.widget.Button代替Button就可以不用那么麻煩的改設置啦,這無疑是一種更好的方法:

<Button
        android:id="@+id/btn_1"

改為:

<android.widget.Button
        android:id="@+id/btn_1"

即可

總結

原文鏈接:https://blog.csdn.net/weixin_52089884/article/details/122616834

欄目分類
最近更新