材质默认为colorAccent而不是colorPrimary

问题描述 投票:1回答:2

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

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

我在清单中将其设置为:

<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作为默认颜色。例如,此按钮:

<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的按钮?

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

在这种情况下,我相信只有按钮中文本的颜色已更改,它具有textColorPrimary的颜色值。textColorPrimary和colorAccent相同的可能性

使用材料成分库1.1.0

或更高版本。

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>

在版本1.0.0中,选择器基于1.0.0

?attr/colorAccent
android material-design android-button material-components-android material-components
2个回答
0
投票

在这种情况下,我相信只有按钮中文本的颜色已更改,它具有textColorPrimary的颜色值。textColorPrimary和colorAccent相同的可能性


0
投票

使用材料成分库1.1.0

© www.soinside.com 2019 - 2024. All rights reserved.