我正在尝试为我的应用程序添加一个夜间主题,我已经浪费了将近三个小时只是试图使文字和图标在我的导航抽屉白色随着黑暗的背景。下面是我在 MainActivity.java
的 onCreate()
中尝试做到这一点的方法:
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
布尔值是不相关的,因为它可以工作。当夜间模式设置为(0) ,任何菜单项选择在导航抽屉变成白色。但是,只有当每个项目被选中时才会发生这种情况,这显然是不方便的。这是我的抽屉。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>