Android在NavigationView中更改单个项目的背景颜色

时间:2018-03-13 09:34:47

标签: java android android-layout navigation-drawer navigationview

如何在导航视图中更改单个项目的背景颜色,即使它未被选中?

2 个答案:

答案 0 :(得分:2)

在导航视图中使用此功能:

function searchForId($id, $array) {
    $did=0;
    $dia=0;
   foreach ($array as $key => $val) {
       if ($val['discount_id'] === $id) {
           $dia +=$val['discount_amount'];
           $did++;
       }
   }
    if($dia != '') {
        echo $dia;
        var_dump($did);
    }
   return null;
};
print_r(searchForId('5a928ce414d488609e73b443',$a));

答案 1 :(得分:1)

创建菜单类:

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

现在在活动课程中:

    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);
      //for single item 
     if (menuItem.getItemId()==R.id.nav_targets){
              menuItem.setTitle(s);   
            }

res / values / styles.xml

<style name="TextAppearance">
    <item name="android:textColor">text_color</item>
    <item name="android:textSize">text_size</item>
</style>

这将设置单项颜色和文本大小。 希望它会帮助你!!