【问题标题】:How to not change Bottom Navigation View Item Color on click如何在单击时不更改底部导航视图项目颜色
【发布时间】:2018-02-03 05:34:43
【问题描述】:

我的bottomNavigationView 中有三件物品

当我单击个人资料项时,代码会检查此人是否已登录。如果此人未登录,那么我需要启动一个新的Activity,否则我需要在我的frameLayout 中加载一个fragment

现在的问题是,当我单击配置文件项并且此人未登录时,活动开始,但是当我单击返回时,配置文件项被突出显示,但主页片段被加载到框架布局中。

我尝试了以下方法来解决这个问题

1) 我使用setSelectedItemId 来设置点击个人资料项目时的项目颜色,但它不起作用

还有其他方法吗?

【问题讨论】:

  • 有没有试过把 setOnNavigationItemReselectedListener(BottomNavigationView.OnNavigationItemReselectedListener listener) 设置一个监听器,当当前选中的底部导航项被重新选中时会得到通知。
  • 这是您如何知道单击了哪个菜单然后使用 setItemIconTintList(ColorStateList tint) 更改其颜色的方法
  • @vikaskumar 我没有重新选择该项目。我在 Home 项上,然后单击 Profile 项。
  • @vikaskumar 我还需要突出显示当前已加载片段的项目。
  • 你能把上面的代码贴出来吗,在这里我不知道你哪里出错了。我得到的是您正在尝试检查天气用户是否已登录然后打开活动或片段。但是,如果您在导航中放置一个配置文件选项卡,则无论如何都应该打开一些片段。您可以在适配器中或在打开第三个选项卡之前的某个位置检查您的状况。您可以在上面放置监听器以了解单击了哪个菜单项,然后在此处进行逻辑以打开活动或配置文件。如果用户未登录,则打开主页片段。

标签: android android-fragments android-fragmentactivity bottomnavigationview


【解决方案1】:

我终于想出了如何做到这一点,我在这里分享它

如果不想改变item的颜色,需要返回false

bottomNavigationView.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.action_home:
                            HomeFragment fav_frag = new HomeFragment();
                            currentFragment = fav_frag;
                            loadfragment(fav_frag);
                            break;
                        case R.id.action_designers:
                          break;
                        case R.id.action_profile:
                            if(PreferenceManager.getDefaultSharedPreferences(BaseActivity.this).getString("customer_id","").equalsIgnoreCase("")){
                                Intent intent=new Intent(BaseActivity.this,LoginActivity.class);
                                intent.putExtra("Target","Profile");
                                startActivity(intent);
                                overridePendingTransition(R.anim.slide_up,R.anim.stable);
// Here is the key , you need to return false when you don't want to change the items color
                                return false;
                            }else {
                                ProfileFragment accountFragment = new ProfileFragment();
                                currentFragment=accountFragment;
                                loadfragment(accountFragment);

                            }


                            break;
                    }
                    return true;
                }
            });

【讨论】:

  • 嗨阿努杰古普塔!你可以试试我的答案,更简单。
【解决方案2】:

试试这个。

selector 添加到您的代码中。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/holo_green_light" android:state_checked="true"/>
    <item android:color="@android:color/black" android:state_checked="false"/>
</selector>

然后添加您的 xml 代码。

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:itemIconTint="@drawable/selector_navigation"
    app:itemTextColor="@drawable/selector_navigation"
    app:menu="@menu/menu_navigation"/>

注意

// icon 
app:itemIconTint="@drawable/selector_navigation"
// text
app:itemTextColor="@drawable/selector_navigation"

【讨论】:

【解决方案3】:

你可以试试

<android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/toolbar_background"
            app:itemIconTint="@color/bottom_nac_color"
            app:itemTextColor="@color/bottom_nac_color"
            app:menu="@menu/bottom_navigation_main" />

这里@color/bottom_nac_color是你想在屏幕上显示的颜色

希望它有效

【讨论】:

    【解决方案4】:

    你可以像这样通过java代码来改变。

    更新:

     bottomNav.addItemNav(new ItemNav(this, R.drawable.ic_home, getResources().getString(R.string.home)).addColorAtive(R.color.yellow_selected_color).addColorInative(R.color.text_color));
    

    【讨论】:

      【解决方案5】:

      return false 就是这样,因为启动活动我们不需要突出显示菜单项

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-15
        • 1970-01-01
        • 2019-05-07
        • 1970-01-01
        • 1970-01-01
        • 2021-12-25
        相关资源
        最近更新 更多