【问题标题】:Set Background Color of Navigation Drawer设置导航抽屉的背景颜色
【发布时间】:2013-06-10 04:05:12
【问题描述】:

我想在java中设置我的导航抽屉的背景颜色,但它似乎是setBackgroundColor,所有类似的方法都没有效果。 只有 XML 行 android:background="@color/mycolor" 有效。如果删除 xml 行并尝试其中一种方法,抽屉将保持透明。

有什么想法吗?

【问题讨论】:

  • 你试过 setBackgroundResource(R.color.mycolor) 吗?
  • 什么意思?来自左边的观点?它采用了您放入的视图的颜色,那么问题是什么?
  • 抱歉没有把代码放在那里。我刚刚解决了我的问题。无论如何谢谢:D

标签: android navigation-drawer


【解决方案1】:

在您的 activity_main.xml 中包含以下内容

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/navigation_drawer_header"
    app:menu="@menu/menu_drawer"
    android:background="@color/color_navigation_list_background"
    app:itemIconTint="@color/color_selector_navigation_item"
    app:itemTextColor="@color/color_selector_navigation_item"/>

【讨论】:

    【解决方案2】:

    我刚刚解决了自己的问题。

    我完全忘记了我要设置背景的不是DrawerLayout,而是里面的ListView

    我承认,如果没有在我的帖子中添加代码,我给你们带来了一些困难 -.-'

    所以而不是:

    private DrawerLayout mDrawerLayout;
    (...)
    mDrawerLayout.setBackgroundResource(int);
    

    我必须这样做:

    private ListView mDrawerList;
    (...)
    mDrawerList.setBackgroundResource(int);
    

    【讨论】:

      【解决方案3】:

      将您的导航抽屉投射到 navigationView 并执行以下操作

       navigationView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
      

      【讨论】:

      • 如何将每个菜单项的背景颜色设置为不同的颜色?
      猜你喜欢
      • 1970-01-01
      • 2018-12-18
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      相关资源
      最近更新 更多