简体   繁体   English

Android 中的 ActionBar 覆盖了 BottomNavigation

[英]ActionBar in Android overrides the BottomNavigation

When trying to change an icon to a downloaded drawable (or actually changing to any other icon during runtime), the icon changes once on the ActionBar.当尝试将图标更改为下载的可绘制对象(或在运行时实际上更改为任何其他图标)时,图标会在 ActionBar 上更改一次。

I actually want to remove the ActionBar and leave only the bottomnav (tabs) for navigation, yet whatever i'm doing the icon changes only on the ActionBar.我实际上想删除 ActionBar 并只保留用于导航的 bottomnav(选项卡),但无论我在做什么,图标只会在 ActionBar 上发生变化。

The item inside bottom_nav_menu.xml: bottom_nav_menu.xml 中的项目:

    <item
    android:id="@+id/navigation_notifications"
    android:icon="@drawable/ic_dashboard_black_24dp"
    android:title="@string/title_notifications"
    app:showAsAction="ifRoom"/>

The code that changes the icon:更改图标的代码:

@Override
public boolean onPrepareOptionsMenu (Menu menu){
    menu.clear();
    getMenuInflater().inflate(R.menu.bottom_nav_menu, menu);
    menu.getItem(2).setIcon(this.bitmap_pic);

    Log.e(TAG, "Icon Changed");
    return super.onPrepareOptionsMenu(menu);
}

The result - Icon stays blank on BottomNav but appears on the ActionBar.结果 - 图标在 BottomNav 上保持空白,但出现在 ActionBar 上。

在此处输入图像描述

Expected result: BottomNav icon will be the image that shown on the top right.预期结果:BottomNav 图标将是右上角显示的图像。

Thanks谢谢

EDIT!编辑!

Issue was fixed after inflating the main_activity layout that contains the BottomNavView在膨胀包含 BottomNavView 的 main_activity 布局后问题已解决

Now the problem the picture isn't showing properly, attached a screenshot (Image is grey instead of showing the icon like in the ActionBar in the first picture):现在问题是图片显示不正确,附上截图(图片是灰色的,而不是像第一张图片中的 ActionBar 那样显示图标):

在此处输入图像描述

Edit 2编辑 2

Icon is still grey instead of showing the bitmap picture.图标仍然是灰色的,而不是显示 bitmap 图片。 Added:添加:

MenuItemCompat.setIconTintMode(bottomNavigationView.getMenu().getItem(2), PorterDuff.Mode.CLEAR);

But it still shows up like in the picture below但它仍然显示如下图

在此处输入图像描述

Edit 3编辑 3

Fixed the issue using:使用以下方法修复了问题:

        bottomNavigationView.setItemIconTintList(null);

I'm not sure if onPrepareOptionsMenu invoked for bottom navigation bar.我不确定是否为底部导航栏调用了onPrepareOptionsMenu

You should have to update navigation menu icon from onCreate method of that Activity.您应该必须从该活动的onCreate方法更新导航菜单图标。

Refer below code,参考下面的代码,

val menu = navigation.menu
val menuItem = menu.findItem(R.id.navigation_notifications) // find particular menu-item using its ID.
menuItem?.icon = this.bitmap_pic

Solution for Gray icon tint, add below line.灰色图标色调的解决方案,添加以下行。

MenuItemCompat.setIconTintMode(menuItem, PorterDuff.Mode.DST)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM