Material 组件默认为 colorAccent 而不是 colorPrimary
Material Components Default to colorAccent instead of colorPrimary
我在 styles.xml
中的 AppTheme
看起来像这样:
<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
?
在这种情况下,我相信只有按钮中的文本颜色发生了变化,它具有 textColorPrimary 的颜色值。
textColorPrimary 和 colorAccent 有可能相同
使用 Material 组件库 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
中,选择器基于 ?attr/colorAccent
:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorAccent" android:state_enabled="true"/>
<item android:color="@color/mtrl_btn_bg_color_disabled"/>
</selector>
首先在您的 styles.xml
或 themes.xml
中创建 btn_style
<style name="btn_style" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">@null</item>
</style>
然后在布局中应用样式并使用您想要的 color
或 drawable
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:background="@color/YOUR_COLOR"
style="@style/btn_style"
android:text="Test"/>
我在 styles.xml
中的 AppTheme
看起来像这样:
<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
?
在这种情况下,我相信只有按钮中的文本颜色发生了变化,它具有 textColorPrimary 的颜色值。 textColorPrimary 和 colorAccent 有可能相同
使用 Material 组件库 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
中,选择器基于 ?attr/colorAccent
:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorAccent" android:state_enabled="true"/>
<item android:color="@color/mtrl_btn_bg_color_disabled"/>
</selector>
首先在您的 styles.xml
或 themes.xml
btn_style
<style name="btn_style" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">@null</item>
</style>
然后在布局中应用样式并使用您想要的 color
或 drawable
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:background="@color/YOUR_COLOR"
style="@style/btn_style"
android:text="Test"/>