代码之家  ›  专栏  ›  技术社区  ›  lannyf

android,添加样式Theme.MaterialComponents.DayNight后,MaterialButton按钮颜色已更改

  •  0
  • lannyf  · 技术社区  · 6 年前

    有材质按钮,样式:

    <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"

    enter image description here

    <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"/>
    

    0 回复  |  直到 6 年前
        1
  •  2
  •   lannyf    6 年前

    添加 "backgroundTint" 成功了,不确定是否有更好的方法:

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