2017-09-25 31 views
2

我想在底部導航視圖中更改所選項目的整個圖標而不是色調顏色。每個圖標都有選擇器,但我不知道要添加到哪裏該圖像選擇器。請任何人告訴我解決方案。Android:底部導航視圖 - 更改所選項目不起作用的圖標

代碼:

private void handleBottomNavigationItemSelected(MenuItem menuItem) { 
    menuItem.setChecked(true); 
    switch (menuItem.getItemId()) { 
     //Replacing the main content with ContentFragment Which is our Inbox View; 
     case R.id.action_calendar: 
      switchFragment(new PatientAppointmentStatusFragment(), "TODAY"); 
      break; 
     case R.id.action_case_sheets: 
      switchFragment(new CaseSheetFragment(), "Case Sheet"); 
      break; 
     case R.id.action_history: 
      switchFragment(new HistoryFragment(), "History"); 
      break; 
     case R.id.action_reports: 
      switchFragment(new ReportsFragment(), "Reports"); 
      break; 
     case R.id.action_billing: 
      switchFragment(new BillingFragment(), "Billing"); 
      break; 

    } 
} 

菜單

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <item 
     android:id="@+id/action_calendar" 
     android:enabled="true" 
     android:icon="@drawable/menu_calendar_bg" 
     android:title="@string/menu_calendar" 
     app:showAsAction="always|withText" /> 
    <item 
     android:id="@+id/action_case_sheets" 
     android:enabled="true" 
     android:icon="@drawable/menu_case_sheets_bg" 
     android:title="@string/menu_case_sheets" 
     app:showAsAction="always|withText" /> 
    <item 
     android:id="@+id/action_history" 
     android:enabled="true" 
     android:icon="@drawable/menu_history_bg" 
     android:title="@string/menu_history" 
     app:showAsAction="always|withText" /> 

    <item 
     android:id="@+id/action_reports" 
     android:enabled="true" 
     android:icon="@drawable/menu_reports_bg" 
     android:title="@string/menu_reports" 
     app:showAsAction="always|withText" /> 

    <item 
     android:id="@+id/action_billing" 
     android:enabled="true" 
     android:icon="@drawable/menu_billing_bg" 
     android:title="@string/menu_billing" 
     app:showAsAction="always|withText" /> 

</menu> 

下面是底部導航視圖的XML代碼。

<BottomNavigationView 
     android:id="@+id/bottom_navigation" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/margin_48dp" 
     android:layout_alignParentBottom="true" 
     app:itemBackground="@drawable/tab_background" 
     app:itemIconTint="@color/navigation_item_color" 
     app:itemTextColor="@color/navigation_item_color" 
     app:menu="@menu/bottom_navigation_main" /> 

我試過以下鏈接,但它不適合我。

鏈接Android: Bottom Navigation View - change icon of selected item

+0

按照這個。 https://materialdoc.com/components/bottom-navigation/ – Radhey

+0

你能告訴我什麼是「navigation_item_color」它是文件或顏色名稱嗎? –

+0

@PRIYAPARASHAR圖標和文本選擇器顏色 –

回答

1

你應該創建一個文件夾顏色資源文件夾.Into RES /彩色文件夾中創建一個文件「navigation_item_color」名稱,並使用下面的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!--state is enabled and checked--> 
    <item android:color="@color/white" android:state_enabled="true" android:state_checked="true" /> 
    <!--state is enabled and not checked--> 
    <item android:color="@color/colorPrimaryDark" android:state_enabled="true" android:state_checked="false" /> 
    <!--state (menu item) is disabled --> 
    <!-- <item android:state_enabled="false" android:color="@color/light_black" />--> 
</selector> 
+0

你好,我已經使用過那樣了。我想更改整個圖標項目 –

+0

這意味着您要更改所有列表項目的圖標。 –

+0

我想用不同的圖標替換選定的項目不是色彩的顏色。 –

相關問題