更改导航抽屉菜单项的颜色

时间:2016-03-03 06:23:46

标签: android navigation-drawer

这是我当前导航抽屉的样子:

enter image description here

我把它分成4组。我尝试的只是给每个群体一个不同的文字颜色。我尝试使用选项SETTINGSFEEDBACKTERMS AND CONDITIONS来获得较小的字体和一点黑色。我搜索过,但无法找到单独自定义导航抽屉组的方法。这是我为菜单编写的代码:

<menu xmlns:android="http://schemas.android.com/apk/res/android">


<group
    android:id="@+id/menu"
    android:checkableBehavior="single">

    <item
        android:id="@+id/nav_targets"
        android:icon="@drawable/icon_target"
        android:title="Targets" />

    <item
        android:id="@+id/nav_testing"
        android:icon="@drawable/icon_testing"
        android:title="Testing" />

    <item
        android:id="@+id/nav_course_work"
        android:icon="@drawable/icon_course_work"
        android:title="Course Work" />

    <item
        android:id="@+id/nav_schedule"
        android:icon="@drawable/icon_schedule"
        android:title="Schedule" />

    <item
        android:id="@+id/nav_profile"
        android:icon="@drawable/icon_profile"
        android:title="Profile" />

</group>

<group
    android:id="@+id/settings">
    <item
        android:title="SETTINGS"
        android:id="@+id/settings_item"></item>
</group>

<group
    android:id="@+id/feedback">
    <item
        android:title="FEEDBACK"
        android:id="@+id/feedback_item"></item>
</group>


<group
    android:id="@+id/TnC">
    <item
        android:title="TERMS &#038; CONDITIONS"
        android:id="@+id/t_n_c_item"></item>
</group>

有没有办法实现它?

4 个答案:

答案 0 :(得分:18)

有两种方法可以单独自定义导航抽屉菜单项。

第一种方式:

MenuItem menuItem = navigationView.getMenu().findItem(R.id.menu_item);
SpannableString s = new SpannableString(menuItem.getTitle());
s.setSpan(new ForegroundColorSpan(TEXT_COLOR), 0, s.length(), 0);
s.setSpan(new AbsoluteSizeSpan(TEXT_SIZE_in_dip, true), 0, s.length(), 0);
menuItem.setTitle(s);

第二种方式:

MenuItem menuItem = navigationView.getMenu().findItem(R.id.menu_item);
SpannableString s = new SpannableString(menuItem.getTitle());
s.setSpan(new TextAppearanceSpan(this, R.style.TextAppearance), 0, s.length(), 0);
menuItem.setTitle(s);

res / values / styles.xml

<style name="TextAppearance">
    <item name="android:textColor">TEXT_COLOR</item>
    <item name="android:textSize">TEXT_SIZE_in_sp</item>
</style>

答案 1 :(得分:1)

这可用于更改导航栏中的所有菜单项颜色。

app:itemTextColor="@android:color/holo_orange_light"

这将在定义导航抽屉的父活动xml文件中定义。在这种情况下使用android.support.v4.widget.DrawerLayout;android.support.design.widget.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:layout_marginTop="@dimen/activity_vertical_margin"
        app:headerLayout="@layout/nav_header_home"
        app:itemTextColor="@android:color/holo_orange_light"
        app:menu="@menu/activity_home_drawer" />

答案 2 :(得分:1)

如果您想为每个选定的-- Just creating a working space create table e ( EmpID int, Location int, Active int ) insert e values (109, 2, 1), (109, 3, 1), (109, 4, 1) declare @csv varchar(100) = '1,2,5' -- Assuming everything is nice integer data select 'Before merge', * from e merge e as target using (select distinct e.EmpID , m.n.value('.[1]', 'varchar(8000)') Location from (select e.* , cast('<xmlroot><rowdata>' + replace(@csv, ',', '</rowdata><rowdata>') + '</rowdata></xmlroot>' as xml) x from e) e cross apply x.nodes('/xmlroot/rowdata') m(n)) as source on target.EmpID = source.EmpID and target.Location = source.Location when matched then update set Active = 1 when not matched then insert (EmpId, Location, Active) values (source.EmpID, source.Location, 1) when not matched by source then update set Active = 0 ; select 'After merge', * from e 添加不同的颜色,则可以使用此选项:

MenuItem

答案 3 :(得分:0)

 <android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/header"
    android:background="your color"
    app:itemTextColor="your color"
    app:itemIconTint="your color"
    app:menu="@menu/drawer" />