android - 更改抽屉导航中菜单项的文本颜色

我正在尝试为我的应用程序添加一个夜间主题,我浪费了近三个小时来尝试让抽屉导航中的文本和图标随着深色背景变为白色。这是我尝试在 MainActivity.javaonCreate() 中执行此操作的方式:

navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

    // This method will trigger onItemClick of navigation menu
    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {

        // Checking if the item is in checked state or not, if not make it in checked state
        if (menuItem.isChecked())
            menuItem.setChecked(false);
        else menuItem.setChecked(true);

        if (nightMode == 0) {
            SpannableString spanString = new SpannableString(menuItem.getTitle().toString());
            spanString.setSpan(new ForegroundColorSpan(Color.WHITE), 0, spanString.length(), 0); // fix the color to white
            menuItem.setTitle(spanString);
        }

nightMode bool 值无关紧要,因为它可以工作。当夜间模式设置为开启 (0) 时,在抽屉导航中选择的任何菜单项都会变为白色。但是,这仅在选择每个项目时才会发生,这显然很不方便。这是我的drawer_dark.xml:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group
        android:checkableBehavior="single">
        <item
            android:id="@+id/unitone"
            android:checked="true"
            android:icon="@drawable/one_white"
            android:title="Classical Period" />
        <item
            android:id="@+id/unittwo"
            android:checked="false"
            android:icon="@drawable/two_white"
            android:title="Postclassical Period" />
        <item
            android:id="@+id/unitthree"
            android:checked="false"
            android:icon="@drawable/three_white"
            android:title="Early Modern Era" />
        <item
            android:id="@+id/unitfour"
            android:checked="false"
            android:icon="@drawable/four_white"
            android:title="Dawn of Industrial Age" />
        <item
            android:id="@+id/unitfive"
            android:checked="false"
            android:icon="@drawable/five_white"
            android:title="Modern Era" />
    </group>
</menu>

我在每个项目的透明背景上使用白色图标,但它们在抽屉导航的黑色背景上显示为黑色。我已经尝试寻找一种 xml 解决方案来更改文本的颜色,但我正在摸不着头脑,因为我不知道为什么会忽略这一点。

有人可以为我提供动态解决方案来实现我想要实现的目标吗?感谢所有帮助,谢谢!

编辑:我没有使用第三方库,它是支持库中提供的 NavigationView。这是 XML 布局:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="7dp"
    tools:context=".MainActivity"
    android:fitsSystemWindows="true" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/ColorDark" />

        <include layout="@layout/toolbar" />

    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:background="#000"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/header"
        app:menu="@menu/drawer" />

</android.support.v4.widget.DrawerLayout>

最佳答案

 <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:background="#000"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/header"
        app:itemTextColor="your color"
        app:menu="@menu/drawer" />

https://stackoverflow.com/questions/32042794/

相关文章:

android - 使用 Glide 库设置完成图像加载后进度条的可见性

android - 如何去除 ImageButton 的标准背景图片?

android - ADB - Android - 获取当前 Activity 的名称

android - 进行动态但方形布局的简单方法

android - 在 Lollipop 崩溃前使用 android vector Drawable

android - 获取 android.content.res.Resources$NotFoun

android - 检查设备是否有摄像头?

Android - fragment 事务上的自定义动画未运行

android - 不同构建风格的不同应用名称?

android - FrameLayout边距不起作用