【问题标题】:android, MaterialButton button color changed after adding style Theme.MaterialComponents.DayNightandroid,添加样式 Theme.MaterialComponents.DayNight 后,MaterialButton 按钮颜色发生变化
【发布时间】:2020-06-17 13:14:10
【问题描述】:

有一个 MaterialButton,样式:

<style name="InstallButtonStyle" parent="Widget.MaterialComponents.Button">
        <item name="materialThemeOverlay">@style/InstallButtonThemeOverlay</item>
    </style>

    <style name="InstallButtonThemeOverlay">
        <item name="colorPrimary">@color/btn_color</item>
    </style>

颜色是蓝色:

<color name="btn_color">#E6188FFF</color>

它一直在工作,但是当在不使用材料的应用程序中使用 aar 时,应用程序崩溃,

android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class com.google.android.material.button.MaterialButton

必须添加 android:theme="@style/Theme.MaterialComponents.DayNight" 到按钮, 应用不再崩溃,但颜色变为紫色

<com.google.android.material.button.MaterialButton
                    android:theme="@style/Theme.MaterialComponents.DayNight"
                    android:id="@id/btn"
                    style="@style/InstallButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="20dp"
                    android:layout_marginBottom="10dp"
                    app:cornerRadius="20dp"
                    android:layout_gravity="bottom|right"
                    android:text="Install Now"
                    android:textAllCaps="false"/>

如何保持样式中定义的颜色?

【问题讨论】:

  • 必须在您的应用中使用 Material Components Theme。

标签: android material-components-android materialbutton


【解决方案1】:

添加"backgroundTint" 使其工作,不确定是否有更好的方法:

<style name="AdInstallButtonStyle" parent="Widget.MaterialComponents.Button">
        <item name="materialThemeOverlay">@style/InstallButtonThemeOverlay</item>
        <item name="backgroundTint">@color/btn_color</item>
    </style>

【讨论】:

    猜你喜欢
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    相关资源
    最近更新 更多