Android 导航抽屉中的自定义颜色

Custom color in navigation drawer in Android

我正在开发一个 Android 应用程序,我必须自定义一个导航抽屉,这是我使用的代码:

<?xml version="1.0" encoding="utf-8"?>
<menu
 xmlns:android="http://schemas.android.com/apk/res/android">    
  <item
   android:title="Discover">      <--- LABEL
   <menu>
    <item
     android:id="@+id/nav_qrcode"         <--- Item of menu
     android:icon="@drawable/read_qr"
     android:title="Read QRCode" />

    <item
     android:id="@+id/nav_favorite"          <--- Item of menu
     android:icon="@drawable/favorite"
     android:title="Favorite" />
   </menu>
 </item>
</menu>

这是他的抽屉布局:

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    android:background="@color/gold"
    app:itemTextColor="@color/white"
    app:menu="@menu/main_drawer" />
</android.support.v4.widget.DrawerLayout>

这是 java 代码:

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.setItemIconTintList(null);

我要自定义导航抽屉,标签的背景必须是"gold"项目"white"。

我该怎么做?

第一种方法,你可以关注这个话题:How to change the Text color of Menu item in Android?

其次,您可以使用列表视图+自定义适配器,然后将此列表视图添加到 DrawerLayout,而不是像那样在 XML 中定义。