材质组件默认为colorAccent而不是colorPrimary [英] Material Components Default to colorAccent instead of colorPrimary

本文介绍了材质组件默认为colorAccent而不是colorPrimary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在styles.xml中的AppTheme看起来像这样:

My AppTheme in styles.xml looks like this:

   <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">@color/textColorPrimary</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>

我在清单中将其设置为:

I set this in Manifest as:

<application
    android:name=".MyApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">

根据 https://material.io/develop/android/components/ 应用于我的窗口小部件的默认颜色应该是定义的colorPrimary,但是我选择的是colorAccent作为默认颜色.例如,此按钮:

According to https://material.io/develop/android/components/ The default color applied to my widgets should be the defined colorPrimary, but mine are picking up colorAccent as the default color. For example, this button:

<com.google.android.material.button.MaterialButton
        android:id="@+id/loginButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="56dp"
        android:layout_marginEnd="56dp"
        android:text="login"
        app:cornerRadius="5dp"
        app:elevation="0dp"
        app:fontFamily="@font/gotham_bold" />

我是否为此项目缺少一个特定的配置,以使该按钮具有显示colorPrimary而不是colorAccent的按钮?

Am I missing a specific config for this project for this to have button to show colorPrimary and not colorAccent?

推荐答案

使用材料组件库 1.1.0 或更高版本.

Use the Material Components Library 1.1.0 or later.

MaterialButton的默认样式为:

    <style name="Widget.MaterialComponents.Button" parent="Widget.AppCompat.Button">
        <item name="backgroundTint">@color/mtrl_btn_bg_color_selector</item>
        <!-- .... -->
    </style>

版本开始 1.1.0 @color/mtrl_btn_bg_color_selector基于?attr/colorPrimary:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="?attr/colorPrimary" android:state_enabled="true"/>
  <item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
</selector>

在版本

这篇关于材质组件默认为colorAccent而不是colorPrimary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆