[英]Android: Change color of navigationView selected item programatically
我创建了一个带有导航视图的抽屉。 在活动的另一部分,我有一个按钮。 当我单击此按钮时,我希望导航视图中的选定项目更改其颜色。 你知道怎么做吗?
编辑:现在我尝试了此:
private void changeNavigationColor(int resColor){
int[][] state = new int[][] {
new int[] {android.R.attr.state_checked} // unchecked
};
int[] color = new int[] {
getResources().getColor(resColor)
};
ColorStateList colorStateList= new ColorStateList(state, color);
navigationView.setItemTextColor(colorStateList.getColorForState());
navigationView.setItemIconTintList(colorStateList);
}
但是它正在更改整个colorListState,而我只想更改它的检查状态
做这个 :
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_dashboard"
app:itemIconTint="@drawable/drawer_item" // add this
app:itemTextColor="@drawable/drawer_item" // add this
app:menu="@menu/activity_dashboard_drawer" />
drawer_item.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="AFTER_CLICK_CHANGE_COLOR_TEXT" android:state_checked="true" />
<item android:color="NAVIGATION_DRAWER_TEXT_COLOR" />
</selector>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.