0

我有三個項目改變底部導航視圖項目顏色當我在輪廓項目單擊該代碼檢查的人是否登錄與否。如果該人沒有登錄,那麼我需要啓動一個新的Activity,否則我需要在我的frameLayout中加載fragment如何不是我<code>bottomNavigationView</code></p> <p><a href="https://i.stack.imgur.com/cGdN9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cGdN9.png" alt="enter image description here"></a></p> <p>上點擊

現在的問題是,當我點擊配置文件項目,而個人沒有登錄,然後活動啓動,但當我點擊後,配置文件項目突出顯示,但主頁片段加載在框架佈局。

enter image description here

我曾嘗試以下辦法來解決這個

1)我用setSelectedItemId設置的項目顏色點擊輪廓項目時,但沒有奏效

有什麼其他方式來做到這一點?

+0

你試過了嗎 setOnNavigationItemReselectedListener(BottomNavigationView.OnNavigationItemReselectedListener listener) 設置一個偵聽器,噹噹前選擇的底部導航項被重新選擇時將會收到通知。 –

+0

這是你如何知道點擊了哪個菜單,然後使用 setItemIconTintList(ColorStateList tint) –

+0

@vikaskumar我不重新選擇項目。我在Home項目上,然後點擊Profile Item。 –

回答

0

你可以嘗試

<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是要顯示在屏幕上的顏色

希望工程

0

試試這個。

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

我終於想通了如何做到這一點,我在這裏分享它

您需要返回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; 
       } 
      }); 
+0

嗨ANUJ GUPTA!你可以試試我的答案。它更容易。 – KeLiuyue

相關問題

 相關問題