我在导航抽屉中更改菜单项标题颜色时遇到问题我设置了itemTextColor但它只更改了项目的颜色而不是菜单的标题.
这是我的
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <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" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" app:itemTextColor="#ffffff" android:background="#283135" /> </android.support.v4.widget.DrawerLayout>
activity_main_drawer.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_home" android:icon="@mipmap/hospital" android:title="Home" /> <item android:id="@+id/nav_reminder" android:icon="@mipmap/alarm" android:title="Reminders" /> </group> <item android:title="Tools"> <menu> <item android:id="@+id/nav_settings" android:icon="@mipmap/settings" android:title="Settings" /> <item android:id="@+id/nav_help" android:icon="@mipmap/information" android:title="Help" /> <item android:id="@+id/nav_about" android:icon="@mipmap/team" android:title="About Us" /> </menu> </item> </menu>
解决方法
给菜单项标题颜色和textSize创建这种方式..
<style name="TextAppearance44"> <item name="android:textColor">#FF0000</item> <item name="android:textSize">20sp</item> </style>
现在在activity_main_drawer.xml文件中为标题赋予id属性..
<item android:title="Tools" android:id="@+id/tools"> <menu> <item android:id="@+id/nav_settings" android:icon="@mipmap/settings" android:title="Settings" /> <item android:id="@+id/nav_help" android:icon="@mipmap/information" android:title="Help" /> <item android:id="@+id/nav_about" android:icon="@mipmap/team" android:title="About Us" /> </menu> </item>
现在在MainActivity.java文件中使用这种方式..
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); Menu menu = navigationView.getMenu(); MenuItem tools= menu.findItem(R.id.tools); SpannableString s = new SpannableString(tools.getTitle()); s.setSpan(new TextAppearanceSpan(this,R.style.TextAppearance44),s.length(),0); tools.setTitle(s); navigationView.setNavigationItemSelectedListener(this);
它会将您的工具颜色更改为Red和TextSize为20sp ..实现它..
在我的情况下输出: