我浪费了将近三个小时,只是试图让我的导航抽屉里的文字和图标变成白色和黑色背景 . 这是我在 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)时,导航抽屉中选择的任何菜单项都将变为白色 . 然而,这仅在选择每个项目时才会发生,这显然是不方便的 . 这是我的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>
14 回答
您只需将主题添加到导航视图即可 .
在style.xml文件中,您将添加此主题
在NavigationView中使用app:itemIconTint,ej:
NavigationView
有一个名为setItemTextColor()
的方法 . 它使用ColorStateList
.Here是我得到答案的地方 . 然后在分配我的NavigationView之后:
更多的选择:
您还可以通过覆盖“textColorSecondary”来更改组 Headers
在styles.xml中
在你的布局中
在您的NavigationView中添加 app:itemTextColor="#fff"
我使用下面的代码更改我的应用程序中的导航抽屉文本颜色 .
在java类中尝试这个
在未来如果有人来这里使用导航抽屉活动(由活动提示窗口中的Studio提供)
答案是 -
在MainActivity中的OnCreate()之前使用它
并在MainActivity中的onNavigationItemSelected()中使用它(如果使用Navigation Drawer活动,则不需要编写此函数,它将添加到MainActivity中) .
提示 - 在onNavigationItemSelected()中的If else Condition之前添加此代码
这适合我 . 取代customTheme,你可以把你的主题放在样式中 . 在此代码中,您还可以更改字体和文本大小 .
这是我的导航视图
您还可以定义从基本主题派生的自定义主题:
然后在styles.xml文件中:
您还可以将更多属性应用于自定义主题 .
使用app:itemBackground设置项目背景
drawer_item_bg.xml
itemIconTint,如果你想改变图标颜色
这可能有助于它对我有用