导航视图似乎改变了背景颜色

Navigation View seems to alter color of background

我希望我的导航视图的背景是#121212,但它变成了更浅的灰色。但是,当应用与背景相同的颜色时,视图中的项目会显示正确的颜色。

android:background="@color/navview"
app:itemBackground="@color/navview"

测试项的背景设置为与导航视图背景相同的颜色,但颜色存在惊人的差异,如下所示。

导航视图背景是否有过滤器?我尝试通过将背景色调模式设置为空来禁用它,

        nvView.backgroundTintMode = null

不过好像没什么效果。感谢您的帮助!

编辑:

<androidx.drawerlayout.widget.DrawerLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/navigation_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/navview"
    android:fitsSystemWindows="true">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nvView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@color/navview"
        app:itemBackground="@color/navview"

        app:menu="@menu/toolbar_menu">


    </com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>

我的导航视图的背景颜色不正确,项目的背景颜色正确。我想要达到的颜色是#121212。​​

您可以使用以下代码片段设置导航视图的背景颜色

navView.setBackgroundColor(ContextCompat.getColor(this,R.color.yourColor)) // yourColor is 
                                                                           // #121212

NavigationView 中,使用 itemShapeFillColor 属性填充项目,android:background 用于菜单视图的背景。

布局中:

<com.google.android.material.navigation.NavigationView
    android:background="#121212"
    app:itemShapeFillColor="@color/..."
    ../>

(我只是在所选项目上使用 alpha 来突出显示它)

您还可以使用样式:

<style name="..." parent="Widget.MaterialComponents.NavigationView" >
   <item name="itemShapeFillColor">@color/....</item>
</style>

只是关于 itemBackground 的注释。

默认样式设置为 @null 以使用由 NavigationViewitemShapeAppearance [=48= 时以编程方式生成的形状背景] itemShapeAppearanceOverlay已设置。
此背景使用 itemShape* 属性设置样式(如 itemShapeFillColoritemShapeInsetStart...)。
设置 itemBackground 将覆盖编程背景并导致 itemShape* 属性中设置的值被忽略。

在我的例子中,接受的答案没有解决我需要的问题,也没有解决我所理解的问题请求:防止 NavigationView 修改提供的背景颜色。

如果其他人需要这个,我通过将以下属性添加到导航视图来实现它:

android:backgroundTintMode="add"
android:backgroundTint="#000"

这会禁用应用于背景的色调,因为它会将 0 添加到当前颜色。