【问题标题】:Android material button taking color primary instead of color accentAndroid材质按钮采用颜色原色而不是颜色重音
【发布时间】:2020-11-09 12:14:04
【问题描述】:

我的布局按钮为 -

<com.google.android.material.button.MaterialButton
  android:id="@+id/save_button"
  style="@style/buttonView"
  android:text="Save"
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintStart_toStartOf="parent" />

在我的styles.xml,我有 -

<style name="buttonView" parent="Theme.MyTheme">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginStart">16dp</item>
        <item name="android:layout_marginLeft">16dp</item>
        <item name="android:layout_marginTop">16dp</item>
        <item name="android:layout_marginEnd">16dp</item>
        <item name="android:layout_marginRight">16dp</item>
    </style>

在我的themes.xml 中,我有 -

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="android:textColorPrimary">@color/black</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!--- Accent color. -->
        <item name="colorAccent">@color/red</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant
        </item>
        <!-- Customize your theme here. -->
    </style>
</resources>

根据 Android 文档,所有 UI 元素(例如 FAB、textview、编辑文本、按钮)都采用颜色强调。所以我希望我的按钮默认采用colorAccent,但为什么它采用colorPrimary。我做错什么了吗?

【问题讨论】:

    标签: android kotlin android-button android-styles material-components-android


    【解决方案1】:

    您可以查看official doc

    填充按钮具有基于colorPrimarybackgroundTint
    同样在您的 buttonView 样式中,您应该扩展提供的样式之一:

    Default style           Widget.MaterialComponents.Button
    Icon style              Widget.MaterialComponents.Button.Icon
    Unelevated style.       Widget.MaterialComponents.Button.UnelevatedButton
    Unelevated icon style   Widget.MaterialComponents.Button.UnelevatedButton.Icon
    

    例子:

    <style name="buttonView" parent="Widget.MaterialComponents.Button">
    
    </style>
    

    如果你想改变背景颜色,你可以:

    • 在您的自定义样式中使用backgroundTint 属性

    • 使用 materialThemeOverlay 覆盖自定义样式中的 colorPrimary 属性(最佳解决方案)

    例子:

    <style name="buttonView"parent="Widget.MaterialComponents.Button">
       <item name="materialThemeOverlay">@style/CustomButtonThemeOverlay</item>
    </style>
    
    <style name="CustomButtonThemeOverlay">
      <item name="colorPrimary">@color/...</item>
    </style>
    

    【讨论】:

      【解决方案2】:

      在我的情况下,当将 android studio 更新到 4.1 版时会出现这个问题。按钮背景颜色,默认情况下总是获得原色紫 500。你的代码没有问题。我将 backgroundTint 属性设置为 null 并将背景属性设置为最喜欢的颜色,或者只是将 backgroundTint 设置为最喜欢的颜色。

      【讨论】:

      • 使用backgroundbackgroundTint属性是不一样的。使用background MaterialButton 不使用自己的MaterialShapeDrawable 作为背景。这意味着形状外观、笔划和圆角等特征将被忽略。
      猜你喜欢
      • 1970-01-01
      • 2020-02-04
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      • 2015-07-07
      • 2015-10-03
      • 2018-02-05
      • 2019-04-26
      相关资源
      最近更新 更多