我在Android的发展初学者。采用了Android默认的抽屉式导航活动我开发的应用程序。一个的这个应用程序的要求,是改变布局的背景颜色(包括导航抽屉标题颜色)随机地在运行时。
现在一切都被罚款会除了在导航抽屉选择的菜单项的颜色仍是蓝色。像这样 :
现在,我想是因为其他布局的背景颜色是粉红色的导航栏上选择的菜单项也应该是粉红色的(我指的是文字的颜色和图标应该是粉红色)所示:
谁能告诉如何以编程方式实现它的代码,因为我有在运行时随意更改所选文本和图标的颜色。
下面是引用菜单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/nav_quiz_all"
android:icon="@drawable/ic_public_black_24dp"
android:checked="true"
android:title="All Countries Quiz"/>
<item
android:id="@+id/nav_quiz_bookmarked"
android:icon="@drawable/ic_favorite_black_24dp"
android:title="Favorite Quiz"/>
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_rate"
android:icon="@drawable/ic_star_black_24dp"
android:title="Rate this app"/>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_share_black_24dp"
android:title="Share"/>
<item
android:id="@+id/nav_feedback"
android:icon="@drawable/ic_feedback_black_24dp"
android:title="Feedback"/>
<item
android:id="@+id/nav_about"
android:icon="@drawable/ic_info_black_24dp"
android:title="About"/>
</menu>
</item>
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings_black_24dp"
android:title="Settings"/>
</menu>
首先检查以下NavigationView
<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/white"
app:itemIconTint="@drawable/drawer_item_color"
app:itemTextColor="@drawable/drawer_item_color"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
点击这里两件事情
app:itemIconTint="@drawable/drawer_item_color"
app:itemTextColor="@drawable/drawer_item_color"
这些两个标记都使用drawer_item_color.xml
这是在可拉伸的文件夹的选择器和下面是它的代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/pink" android:state_checked="true" />
<item android:color="@color/black" />
</selector>
使用选择并添加你想要的颜色。这会为你做这项工作。
第一种方式
尝试使用:
app:itemIconTint="@color/color_pink" //selected icon color
app:itemTextColor="@color/color_pink" //selected text color
app:itemBackground="@color/color_gray"
为了您的NavigationView
<android.support.design.widget.NavigationView
android:id="@+id/navigation_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header_layout"
app:itemIconTint="@color/color_pink"
app:itemTextColor="@color/color_pink"
app:itemBackground="@color/color_gray"
app:menu="@menu/menu_drawer" />
第二种方式
对于编程方式更改用途:
navigationView.setItemTextColor(ColorStateList1);
navigationView.setItemIconTintList(ColorStateList2);
定义ColorStateList1
和ColorStateList2
为:
对于导航查看项目文本颜色
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.BLUE,
Color.WHITE,
Color.WHITE
};
ColorStateList ColorStateList1 = new ColorStateList(state, 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.BLUE,
Color.WHITE,
Color.WHITE
};
ColorStateList ColorStateList2 = new ColorStateList(states, colors);
首先感谢大家对你的解决方案应对回来:)从上面的答案和学习上做ColorStateList
我终于成功地创建一个method
它设置在导航抽屉检查商品的颜色,以配合我的应用程序的主题颜色一些研究这是在运行时随机生成的颜色。
这里的方法:
public void setNavMenuItemThemeColors(int color){
//Setting default colors for menu item Text and Icon
int navDefaultTextColor = Color.parseColor("#202020");
int navDefaultIconColor = Color.parseColor("#737373");
//Defining ColorStateList for menu item Text
ColorStateList navMenuTextList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked},
new int[]{android.R.attr.state_enabled},
new int[]{android.R.attr.state_pressed},
new int[]{android.R.attr.state_focused},
new int[]{android.R.attr.state_pressed}
},
new int[] {
color,
navDefaultTextColor,
navDefaultTextColor,
navDefaultTextColor,
navDefaultTextColor
}
);
//Defining ColorStateList for menu item Icon
ColorStateList navMenuIconList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked},
new int[]{android.R.attr.state_enabled},
new int[]{android.R.attr.state_pressed},
new int[]{android.R.attr.state_focused},
new int[]{android.R.attr.state_pressed}
},
new int[] {
color,
navDefaultIconColor,
navDefaultIconColor,
navDefaultIconColor,
navDefaultIconColor
}
);
mNavView.setItemTextColor(navMenuTextList);
mNavView.setItemIconTintList(navMenuIconList);
}
你可以调用这个方法你想要的任何int color
:)
我已经测试上SDK 17此代码到SDK 26码写逻辑后
setContent(R.layou.your_activity)
找到ID为导航视图
private void setupNavigationSelection(NavigationView navigationViewList) {
/* note : warning don't use other attribute just checked and unchecked else wont work*/
int[][] states = new int[][]{
new int[]{android.R.attr.state_checked}, // checked
new int[]{-android.R.attr.state_checked} // unchecked
};
int[] colors = new int[]{
Color.RED
Color.GREEN,
};
LayerDrawable layerDrawable = getSideBarDrawable(0x80dedede);
StateListDrawable arrowImgStates = new StateListDrawable();
Drawable normalDrawable = new ColorDrawable(Color.TRANSPARENT);;
arrowImgStates.addState(new int[]{android.R.attr.state_pressed}, normalDrawable);
arrowImgStates.addState(new int[]{android.R.attr.state_focused}, layerDrawable);
arrowImgStates.addState(new int[]{android.R.attr.state_selected}, layerDrawable);
arrowImgStates.addState(new int[]{android.R.attr.state_checked}, layerDrawable);
arrowImgStates.addState(new int[]{}, normalDrawable);
ColorStateList colorStateList = new ColorStateList(states, colors);
navigationViewList.setItemTextColor(colorStateList);
navigationViewList.setItemIconTintList(colorStateList);
navigationViewList.setItemBackground(arrowImgStates);
}
public LayerDrawable getSideBarDrawable(int bgColor) {
int iSize = CLViewUtil.dpToPx(6);
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setStroke(iSize, CLThemeUtil.getThemePrimaryColor(this));
gradientDrawable.setColor(bgColor);
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{gradientDrawable});
layerDrawable.setLayerInset(0, 0, -iSize, -iSize, -iSize);
return layerDrawable;
}
你有没有尝试过?
mNavigationView.setItemTextColor(yourColorStateList);