FAB的颜色什么时候依赖于colorAccent?
When does FAB's color depend on colorAccent?
根据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 中是只读的。
您唯一能做的就是切换主题或手动设置每个组件的颜色。
注意: 您可以将主题应用到 UI 的一部分而不是整个 Activity 以更改强调色(或其他东西)本地。为此,您可以通过 AppCompat 库在 XML 布局中使用 android:theme 属性,或者您可以通过提供 ContextThemeWrapper 作为 LayoutInflater 的上下文来扩充布局。
或
<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>
并在 activity 设置主题内
setTheme(R.style.AppTheme_NewTheme);
setContentView(R.layout.activity_main);
从 Material 组件库的 version 1.1.0
开始,FloatingActionButton
的默认样式基于 ?attr/colorSecondary
:
<style name="Widget.MaterialComponents.FloatingActionButton" parent="Widget.Design.FloatingActionButton">
<item name="backgroundTint">?attr/colorSecondary</item>
</style>
从 Material 组件库的版本 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
:
<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
中,backgroundTint 基于 ?attr/colorAccent
:
<style name="Widget.Design.FloatingActionButton" parent="android:Widget">
<item name="backgroundTint">?attr/colorAccent</item>
...
</style>
我想here你可以阅读更多:
Component Attribute | Default Theme Attribute Value
- backgroundTint | colorSecondary
- tint | colorOnSecondary
- rippleColor | colorOnSecondary
或者您可以在 XML 中直接更改为 app:backgroundTint
:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floating_action_button"
style="@style/Widget.Design.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
app:backgroundTint="@color/colorAccent"
app:srcCompat="@drawable/ic_plus_24"/>
根据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 中是只读的。
您唯一能做的就是切换主题或手动设置每个组件的颜色。
注意: 您可以将主题应用到 UI 的一部分而不是整个 Activity 以更改强调色(或其他东西)本地。为此,您可以通过 AppCompat 库在 XML 布局中使用 android:theme 属性,或者您可以通过提供 ContextThemeWrapper 作为 LayoutInflater 的上下文来扩充布局。
或
<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>
并在 activity 设置主题内
setTheme(R.style.AppTheme_NewTheme);
setContentView(R.layout.activity_main);
从 Material 组件库的 version 1.1.0
开始,FloatingActionButton
的默认样式基于 ?attr/colorSecondary
:
<style name="Widget.MaterialComponents.FloatingActionButton" parent="Widget.Design.FloatingActionButton">
<item name="backgroundTint">?attr/colorSecondary</item>
</style>
从 Material 组件库的版本 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
:
<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
中,backgroundTint 基于 ?attr/colorAccent
:
<style name="Widget.Design.FloatingActionButton" parent="android:Widget">
<item name="backgroundTint">?attr/colorAccent</item>
...
</style>
我想here你可以阅读更多:
Component Attribute | Default Theme Attribute Value
- backgroundTint | colorSecondary
- tint | colorOnSecondary
- rippleColor | colorOnSecondary
或者您可以在 XML 中直接更改为 app:backgroundTint
:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floating_action_button"
style="@style/Widget.Design.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
app:backgroundTint="@color/colorAccent"
app:srcCompat="@drawable/ic_plus_24"/>