42

I wanted to set the background color of my Navigation drawer in java but it seems as setBackgroundColor and all similar methods have no effect. Only the XML line android:background="@color/mycolor" is working. If remove the xml line and try one of the methods the drawer just stays transparent.

Any ideas?

4

3 回答 3

85

在您的 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"/>
于 2015-11-02T08:24:57.527 回答
28

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

我完全忘记了DrawerLayout我想要设置背景的不是内部,而是ListView内部。

我承认我在不向我的帖子中添加代码的情况下让你们有些困难-.-'

所以而不是:

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

我不得不这样做:

private ListView mDrawerList;
(...)
mDrawerList.setBackgroundResource(int);
于 2013-06-13T17:33:42.850 回答
4

将您的导航抽屉转换为 navigationView 并执行以下操作

 navigationView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
于 2018-08-07T13:12:21.797 回答