Android材质按钮采用颜色为主色而不是颜色强调色 [英] Android material button taking color primary instead of color accent
问题描述
我的布局按钮为-
<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,edit text,button)都带有颜色.因此,我希望我的按钮默认采用colorAccent
,但是为什么要采用colorPrimary
.我做错什么了吗?
As per the Android documentation all the UI elements such as FAB, textview, edit text, button take the color accent. So I expect my button to take the colorAccent
by default but why does it take colorPrimary
. Am I doing anything wrong?
推荐答案
您可以检查填充的按钮具有基于 colorPrimary
的backgroundTint
.
同样,在您的buttonView
样式中,您应该扩展所提供的样式之一:
The filled button has the backgroundTint
based on the colorPrimary
.
Also in your buttonView
style you should extend one of the provided styles:
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>
如果要更改背景颜色,可以:
If you want to change the background color you can:
-
以自定义样式使用
backgroundTint
属性
使用 materialThemeOverlay
(最佳解决方案)
Override the colorPrimary
attribute in your custom style using the materialThemeOverlay
(best solution)
示例:
<style name="buttonView"parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">@style/CustomButtonThemeOverlay</item>
</style>
<style name="CustomButtonThemeOverlay">
<item name="colorPrimary">@color/...</item>
</style>
这篇关于Android材质按钮采用颜色为主色而不是颜色强调色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!