Android中的导航抽屉菜单项标题颜色 [英] Navigation Drawer Menu Item Title Color in Android

查看:84
本文介绍了Android中的导航抽屉菜单项标题颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置itemTextColorNavigation Drawer中更改菜单项标题Color时遇到问题,但它仅更改了项目的Color,而不是菜单的Title.

这是我的 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>

检查此内容:

解决方案

给出Menu项目title colortextSize的方式.

将此添加到您的styles.xml文件中.

 <style name="TextAppearance44">
        <item name="android:textColor">#FF0000</item>
        <item name="android:textSize">20sp</item>
 </style>

现在在activity_main_drawer.xml文件中,将id attribute赋予title.

<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), 0, s.length(), 0);
        tools.setTitle(s);
        navigationView.setNavigationItemSelectedListener(this);

它将把您的tools color更改为Red,将TextSize更改为20sp.

在我的情况下,输出:

I have a problem changing the Menu Item Title Color in the Navigation Drawer I set the itemTextColor but it only changes the Color of the Items not the Title of the menu.

Here is my 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>

Check this:

解决方案

To give Menu item title color and textSize Create this way..

add this to your styles.xml file.

 <style name="TextAppearance44">
        <item name="android:textColor">#FF0000</item>
        <item name="android:textSize">20sp</item>
 </style>

now in activity_main_drawer.xml file give id attribute to title..

<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>

now In MainActivity.java file use this way..

 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), 0, s.length(), 0);
        tools.setTitle(s);
        navigationView.setNavigationItemSelectedListener(this);

It will change your tools color to Red and TextSize to 20sp.. Implement it..

In my case Output :

这篇关于Android中的导航抽屉菜单项标题颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆