【问题标题】:Navigation View seems to alter color of background导航视图似乎改变了背景颜色
【发布时间】:2020-03-20 09:25:30
【问题描述】:

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

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

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

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

        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。​​

【问题讨论】:

    标签: android kotlin material-components material-components-android android-navigationview


    【解决方案1】:

    在我的情况下,接受的答案并不能解决我的需要,以及我理解的问题要求:防止 NavigationView 修改提供的背景颜色。

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

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

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

    【讨论】:

      【解决方案2】:

      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,以便在设置itemShapeAppearance和/或itemShapeAppearanceOverlay时使用NavigationView以编程方式生成的形状背景。
      此背景的样式使用 itemShape* 属性(如 itemShapeFillColoritemShapeInsetStart...)。
      设置itemBackground 将覆盖编程背景并导致itemShape* 属性中设置的值被忽略。

      【讨论】:

      • 我确实理解为什么导航视图中的项目是这样着色的,但是我的问题是我希望导航背景与项目的颜色相同。相反,它显示为 #121212 的浅灰色变体
      • 发布您的布局和颜色/选择器
      • @A.D 刚刚使用 1.1.0-beta02 和 1.2.0-alpha02 尝试了答案中的代码,它使用正确的颜色。更新了答案。
      • @GabrieleMariotti 如果我使用itemShapeFillColor,那么所有项目(不仅选择一个)的背景颜色都设置为该颜色。我怎样才能让它只用于选定的项目?
      【解决方案3】:

      您可以使用以下代码的 sn-p 设置导航视图的背景颜色

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-22
        • 2011-02-03
        • 1970-01-01
        • 2015-04-18
        相关资源
        最近更新 更多