问题描述
我想加一个晚上的主题为我的应用程序,我已经浪费了近三个小时只是试图让我的抽屉式导航栏的文字和图标变为白色与黑色背景。这是我想要去的的onCreate这样做()
在 MainActivity.java
的方式:
navigationView =(NavigationView)findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(新NavigationView.OnNavigationItemSelectedListener(){
//此方法将触发导航菜单onItemClick
@覆盖
公共布尔onNavigationItemSelected(菜单项菜单项){
//检查,如果该项目处于选中状态或没有,如果不让它在选中状态
如果(menuItem.isChecked())
menuItem.setChecked(假);
其他menuItem.setChecked(真正的);
如果(夜间模式== 0){
SpannableString spanString =新SpannableString(menuItem.getTitle()的toString());
spanString.setSpan(新ForegroundColorSpan(Color.WHITE),0,spanString.length(),0); //固定颜色为白色
menuItem.setTitle(spanString);
}
在夜间模式
布尔是无关紧要的,因为它的工作原理。当夜间模式设置为on(0),在导航抽屉中选定的任何菜单项变为白色。但是,当每个项被选中这显然是不方便的只发生。这是我的drawer_dark.xml:
< XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android>
<组
机器人:checkableBehavior =单一>
<项目
机器人:ID =@ + ID / unitone
机器人:检查=真
机器人:图标=@可绘制/ one_white
机器人:标题=古典时期/>
<项目
机器人:ID =@ + ID / unittwo
机器人:检查=假
机器人:图标=@可绘制/ two_white
机器人:标题=后经典时代/>
<项目
机器人:ID =@ + ID / unitthree
机器人:检查=假
机器人:图标=@可绘制/ three_white
机器人:标题=近代早期时代/>
<项目
机器人:ID =@ + ID / unitfour
机器人:检查=假
机器人:图标=@可绘制/ four_white
机器人:标题=黎明工业时代的/>
<项目
机器人:ID =@ + ID / unitfive
机器人:检查=假
机器人:图标=@可绘制/ five_white
机器人:标题=近现代/>
< /组>
< /菜单>
我使用的是透明背景每个项目的白色图标,但他们显示为黑色的导航抽屉的黑色背景。我试着寻找一个XML解决方案来改变文字的颜色,我抓我的头,因为我不知道为什么,这是忽视了。
有人可以提供我得到我想要实现一个动态的解决方案吗?所有帮助AP preciated,谢谢!
编辑:我不使用第三方库,它在支持库提供的NavigationView。这里的XML布局:
< android.support.v4.widget.DrawerLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
的xmlns:程序=http://schemas.android.com/apk/res-auto
机器人:ID =@ + ID /抽屉
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:海拔=7DP
工具:上下文=。MainActivity
机器人:fitsSystemWindows =真正的>
<的FrameLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的FrameLayout
机器人:ID =@ + ID /容器
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=@色/ ColorDark/>
<包括布局=@布局/工具栏/>
< /的FrameLayout>
< android.support.design.widget.NavigationView
机器人:ID =@ + ID / navigation_view
机器人:后台=#000
机器人:layout_height =match_parent
机器人:layout_width =match_parent
机器人:layout_gravity =开始
应用程序:headerLayout =@布局/头
应用程序:菜单=@菜单/抽屉/>
< /android.support.v4.widget.DrawerLayout>
NavigationView
的 setItemTextColor()称为方法
。它采用了 ColorStateList
。
//用于导航视图项的文字颜色
INT [] []状态=新的INT [] [] {
新的INT [] {} -android.R.attr.state_enabled,//禁用
新的INT [] {} android.R.attr.state_enabled,//启用
新的INT [] {} -android.R.attr.state_checked,//选中
新的INT [] {android.R.attr.state_ pressed} // pressed
};
INT []颜色=新INT [] {
Color.WHITE,
Color.WHITE,
Color.WHITE,
Color.WHITE
};
ColorStateList CSL =新ColorStateList(州,颜色);
//用于导航视图项目图标的颜色
INT [] []状态=新的INT [] [] {
新的INT [] {} -android.R.attr.state_enabled,//禁用
新的INT [] {} android.R.attr.state_enabled,//启用
新的INT [] {} -android.R.attr.state_checked,//选中
新的INT [] {android.R.attr.state_ pressed} // pressed
};
INT []颜色=新INT [] {
Color.WHITE,
Color.WHITE,
Color.WHITE,
Color.WHITE
};
ColorStateList CSL2 =新ColorStateList(州,颜色);
这里是在那里我得到这个问题的答案。并指派我NavigationView后,然后用鼠标右键:
如果(夜间模式== 0){
navigationView.setItemTextColor(CSL);
navigationView.setItemIconTintList(CSL2);
}
I'm trying to add a night theme for my app and I've wasted nearly three hours just trying to make the text and icons in my navigation drawer turn white along with the dark background. Here is the way I'm trying to go about doing this in onCreate()
in MainActivity.java
:
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);
}
The nightMode
boolean is irrelevant because it works. When night mode is set to on (0), whatever menu item is selected in the navigation drawer turns white. However, that only happens when each item is selected which is obviously inconvenient. Here is my 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>
I'm using white icons on a transparent background for each item, yet they show up as black on the black background of the navigation drawer. I've tried looking for an xml solution to changing the color of the text and I'm scratching my head because I don't know why this was overlooked.
Can someone offer me a dynamic solution in getting what I'm trying to achieve? All help is appreciated, thank you!
EDIT: I am not using a third party library, it's the NavigationView provided in the support library. Here's the XML layout:
<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>
NavigationView
has a method called setItemTextColor()
. It uses a ColorStateList
.
// FOR NAVIGATION VIEW ITEM TEXT COLOR
int[][] state = new int[][] {
new int[] {-android.R.attr.state_enabled}, // disabled
new int[] {android.R.attr.state_enabled}, // enabled
new int[] {-android.R.attr.state_checked}, // unchecked
new int[] { android.R.attr.state_pressed} // pressed
};
int[] color = new int[] {
Color.WHITE,
Color.WHITE,
Color.WHITE,
Color.WHITE
};
ColorStateList csl = new ColorStateList(state, color);
// FOR NAVIGATION VIEW ITEM ICON COLOR
int[][] states = new int[][] {
new int[] {-android.R.attr.state_enabled}, // disabled
new int[] {android.R.attr.state_enabled}, // enabled
new int[] {-android.R.attr.state_checked}, // unchecked
new int[] { android.R.attr.state_pressed} // pressed
};
int[] colors = new int[] {
Color.WHITE,
Color.WHITE,
Color.WHITE,
Color.WHITE
};
ColorStateList csl2 = new ColorStateList(states, colors);
Here is where I got that answer. And then right after assigning my NavigationView:
if (nightMode == 0) {
navigationView.setItemTextColor(csl);
navigationView.setItemIconTintList(csl2);
}
这篇关于更改菜单项的文本颜色的抽屉式导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!