美文网首页
【Android】关于Button背景色/样式设置失效

【Android】关于Button背景色/样式设置失效

作者: 呼语 | 来源:发表于2021-05-11 15:20 被阅读0次

    一、问题描述

    在设置button背景颜色后,button背景仍然为蓝色。如下所示:

    <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+ 进行开发时,创建的项目默认的主题是Theme.MaterialComponents.DayNight.DarkActionBar
    。所有Button都是Material类型的Button,默认使用主题色。

    解决方法:

    1. 将主题切换为:Theme.AppCompat.*** , 不使用Material控件
    <style name="Theme.ShapePractice" parent="Theme.AppCompat.DayNight.DarkActionBar">
    
    1. 设置 android:backgroundTint="@null" 着色设置为null后再进行背景设置
    <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

    相关文章

      网友评论

          本文标题:【Android】关于Button背景色/样式设置失效

          本文链接:https://www.haomeiwen.com/subject/ifzgdltx.html