【问题标题】:Android: Bottom Navigation View - change icon of selected item not workingAndroid:底部导航视图 - 更改所选项目的图标不起作用
【发布时间】:2018-03-06 07:07:40
【问题描述】:

我想更改所选项目的整个图标而不是底部导航视图中的色调。我有每个图标的选择器,但我不知道在哪里添加该图像选择器。请任何人告诉我解决方案。

代码:

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

【问题讨论】:

  • 请告诉我什么是“navigation_item_color” 是文件名还是颜色名?
  • @PRIYAPARASHA 图标和文本选择器颜色
  • @Radhey 任何想法改变所选项目的不同图标?
  • 您的预期输出是什么,您目前得到了什么!请附上屏幕。

标签: android bottomnavigationview


【解决方案1】:

你应该在res文件夹中创建一个color文件夹。进入res/color文件夹创建一个文件" 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>

【讨论】:

  • 您好,我已经在使用这种方式了。我想更改整个图标项
  • 表示要更改所有列表项的图标。
  • 我想用不同的图标而不是色调替换所选项目。
  • 为了替换图标然后你可以使用@drawable/ 而不是@color/
  • 是的,我试过了,它不起作用。你有任何代码吗?
猜你喜欢
  • 2017-06-09
  • 2021-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多