我有三个项目改变底部导航视图项目颜色当我在轮廓项目单击该代码检查的人是否登录与否。如果该人没有登录,那么我需要启动一个新的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>上点击
现在的问题是,当我点击配置文件项目,而个人没有登录,然后活动启动,但当我点击后,配置文件项目突出显示,但主页片段加载在框架布局。
我曾尝试以下办法来解决这个
1)我用setSelectedItemId
设置的项目颜色点击轮廓项目时,但没有奏效
有什么其他方式来做到这一点?
你可以尝试
<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
是要显示在屏幕上的颜色
希望工程
试试这个。
将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"
我终于想通了如何做到这一点,我在这里分享它
您需要返回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;
}
});
嗨ANUJ GUPTA!你可以试试我的答案。它更容易。 – KeLiuyue
你试过了吗 setOnNavigationItemReselectedListener(BottomNavigationView.OnNavigationItemReselectedListener listener) 设置一个侦听器,当当前选择的底部导航项被重新选择时将会收到通知。 –
这是你如何知道点击了哪个菜单,然后使用 setItemIconTintList(ColorStateList tint) –
@vikaskumar我不重新选择项目。我在Home项目上,然后点击Profile Item。 –