【问题标题】:Material design components button global theme overriding broken材料设计组件按钮全局主题覆盖破碎
【发布时间】:2019-10-11 07:05:47
【问题描述】:

我想简单地将样式应用于这样的主题级别上的所有按钮

<style name="BaseTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
     ...    
    <item name="buttonStyle">@style/DefaultButton</item>
</style>

<style name="DefaultButton" parent="Widget.MaterialComponents.Button">
    <item name="android:textColor">@color/whatever</item>
</style>

<Button
    android:id="@+id/addChannelButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center_horizontal"
    android:layout_margin="16dp"
    android:text="Add room" />

为什么这不起作用?它会在 appcompat 中

// 如果我使用Theme.MaterialComponents.Light.NoActionBar.Bridge,那么它可以工作

【问题讨论】:

    标签: android material-design android-button material-components material-components-android


    【解决方案1】:

    您应该在主题中设置materialButtonStyle 而不是buttonStyle

    【讨论】:

    • 有效,但是它现在忽略了我的背景设置(自定义形状可绘制),我有什么方法可以通过 materialButtonStyle 覆盖所有内容? (当使用桥接主题和按钮样式时,背景工作)
    【解决方案2】:

    使用定义materialButtonStyle 属性的Theme.MaterialComponents 主题。

      <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.MaterialComponents.Light">
        ....
    
        <item name="materialButtonStyle">@style/MyButtonTheme</item>
      </style>
    

    通过这种方式,您可以自定义应用中所有按钮的主题。
    同样从材质库的 1.1.0 版本开始,您可以使用 materialThemeOverlay 属性覆盖默认样式的主题属性。

    类似:

      <style name="MyButtonTheme" parent="Widget.MaterialComponents.Button">
        <item name="materialThemeOverlay">@style/ButtonStyleTextColor</item>
      </style>
    
      <style name="ButtonStyleTextColor">
        <!-- For filled buttons, your theme's colorPrimary provides the default background color of the component, and -->
        <!--the text color is colorOnPrimary -->
        <item name="colorOnPrimary">@color/my_color</item>
    
      </style>
    

    目前安卓库需要材料组件1.1.0版本。

    【讨论】:

      【解决方案3】:

      您可以尝试使用 v1.0.0-alpha06 吗?自 v1.0.0 以来已经取得了一些进展,这可能已经解决了您所看到的问题。

      1.1.0-alpha02

      • 形状主题:FloatingActionButton、MaterialButton、Chip、MaterialCardView、BottomSheet 和 TextInputLayout 已更新为使用新的 Material 形状系统

      1.1.0-alpha06

      • 在 MaterialButton 中实现 Shapeable 接口

      来源:https://github.com/material-components/material-components-android/releases

      【讨论】:

        【解决方案4】:

        使用materialButtonStyle 并使用MaterialButton 而不是Button

        【讨论】:

          猜你喜欢
          • 2015-01-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-01
          • 2019-05-23
          相关资源
          最近更新 更多