为什么不更改BottomNavigationView的颜色? [英] Why doesn't change the color of BottomNavigationView?
问题描述
所以,我的问题是,当我在应用程序中更改主题时,除了MainActivity BottomNavigationView之外,它在所有片段上都能正常工作.
So, my problem is that, when I change the theme in the application, it works well on all the fragments, except in the MainActivity BottomNavigationView.
我认为它不是使用深色主题颜色,而只是使用浅色主题颜色,我也不知道为什么.
I think it isn't use the dark theme colors, just use only the light theme colors, and I don't know why.
我尝试了Stackoverflow的一些解决方案,但我不知道出了什么问题.
I tried some solutions from Stackoverflow but I can't figure up what's wrong.
这是我的MainActivity
This is my MainActivity
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?attr/BottomMenuBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu"
app:itemIconTint="@color/bnv_tab_item_foreground"
app:itemTextColor="@color/bnv_tab_item_foreground" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>```
This is my style based on attributes
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#e0e0e0</item>
<item name="colorPrimaryDark">#000000</item>
<item name="colorAccent">#9e9e9e</item>
<item name="backgroundColor">#eeeeee</item>
<item name="warningButtonColor">#b71c1c</item>
<item name="okButtonColor">#2196F3</item>
<item name="textColor">#000000</item>
<item name="disabledTextColor">#BDBDBD</item>
<item name="bottomMenuTextColor">#9e9e9e</item>
<item name="BottomMenuBackground">#e0e0e0</item>
<item name="BottomMenuTintColor">#000000</item>
<item name="BottomMenuCheckedTextColor">#ffffff</item>
<item name="dividerColor">#000000</item>
</style>
<style name="darkTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#212121</item>
<item name="colorPrimaryDark">#000000</item>
<item name="colorAccent">#202020</item>
<item name="backgroundColor">#101010</item>
<item name="warningButtonColor">#b71c1c</item>
<item name="okButtonColor">#2196F3</item>
<item name="disabledTextColor">#424242</item>
<item name="textColor">#fafafa</item>
<item name="bottomMenuTextColor">#424242</item>
<item name="BottomMenuBackground">#000000</item>
<item name="BottomMenuTintColor">#dd2c00</item>
<item name="BottomMenuCheckedTextColor">#dd2c00</item>
<item name="dividerColor">#dd2c00</item>
</style>
</resources>
And finally my selector of checked button
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="?attr/BottomMenuCheckedTextColor" />
<item android:state_checked="false" android:color="?attr/bottomMenuTextColor"/>
</selector>
推荐答案
要使用深色模式,请先更改主题父级:
To use Dark Mode first change theme parent:
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
如果要替换应用程序中的颜色,则需要创建一个名为values-night的自定义值目录.
If you want to replace the color in the app, you need to create a custom values directory, called values-night.
灯光模式:
<color name="colorPrimary">#e0e0e0</color>
<color name="colorPrimaryDark">#000000</color>
<color name="colorAccent">#9e9e9e</color>
黑暗模式:
<color name="colorPrimary">#212121</color>
<color name="colorPrimaryDark">#000000</color>
<color name="colorAccent">#202020</color>
这篇关于为什么不更改BottomNavigationView的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!