FAB的颜色何时取决于colorAccent?

问题描述 投票:0回答:3

根据doc,视图的颜色默认为我们主题的colorAccent。我尝试在样式资源文件中更改colorAccent,但没有任何反应:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="colorAccent">#d81b60</item>
</style>

所以我尝试改为更改colorSecondary

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="colorSecondary">#d81b60</item>
</style>

而且有效。所以我的问题是,它真的取决于colorAccent吗?如果是,何时?

android android-studio android-theme floating-action-button
3个回答
2
投票

[从材料成分库的version 1.1.0开始,1.1.0的默认样式基于FloatingActionButton

?attr/colorSecondary

[从材料组件库的版本 <style name="Widget.MaterialComponents.FloatingActionButton" parent="Widget.Design.FloatingActionButton"> <item name="backgroundTint">?attr/colorSecondary</item> </style> 开始,1.2.0的默认样式是:

1.2.0

从...开始版本FloatingActionButton <style name="Widget.MaterialComponents.FloatingActionButton" parent="Widget.Design.FloatingActionButton"> <item name="backgroundTint">@color/mtrl_fab_bg_color_selector</item> <!-- .... --> </style> 基于1.2.0

@color/mtrl_fab_bg_color_selector

在版本?attr/colorSecondary中,backgroundTint基于<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="?attr/colorSecondary" android:state_enabled="true"/> <item android:alpha="0.12" android:color="?attr/colorOnSurface"/> </selector>

1.0.0

0
投票

不,您不能,因为在主题中定义了强调色,并且主题在Android中是只读的。

您唯一可以做的就是切换主题或手动设置每个组件的颜色。

注意:您可以将主题应用于UI的一部分而不是整个Activity,以便在本地更改强调色(或其他内容)。为此,可以将XML布局中的android:theme属性与AppCompat库一起使用,也可以通过将ContextThemeWrapper作为ContextInmeWrapper提供给LayoutInflater来使布局膨胀。

OR

1.0.0

和活动集主题内

?attr/colorAccent

0
投票

我想 <style name="Widget.Design.FloatingActionButton" parent="android:Widget"> <item name="backgroundTint">?attr/colorAccent</item> ... </style> ,您可以阅读更多:

组件属性默认主题属性值-backgroundTint colorSecondary-tint colorOnSecondary-rippleColor colorOnSecondary

或您可以使用<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> </style> <style name="AppTheme.NewTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/colorOne</item> <item name="colorPrimaryDark">@color/colorOneDark</item> </style> 直接在XML中进行更改:

 setTheme(R.style.AppTheme_NewTheme);
 setContentView(R.layout.activity_main);
© www.soinside.com 2019 - 2024. All rights reserved.