我为我的应用程序添加了底部导航视图,但我需要活动之间的底部导航视图而不是片段,因此我已将此代码添加到 Java 中以用于我的所有 3 个活动。
当我在手机中选择“第二”或“第三”时,一切都正确,但问题是突出显示第一项。
我需要突出显示我按下的项目。
我已经使用了片段,它工作得很好,但我仍然是使用片段的初学者,所以我正在使用活动。
第一个活动代码是:
BottomNavigationView mBottomNavigation;
mBottomNavigation =(BottomNavigationView) findViewById(R.id.BottomNavigator);
mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.Nav_Second:
Intent Second= new Intent(First.this, Second.class);
startActivity(Second);
break;
case R.id.Nav_Third:
Intent Third= new Intent(First.this, Third.class);
startActivity(Third);
break;
}
return true;
}
});
}}
第二个活动是:
底部导航视图 mBottomNavigation;
mBottomNavigation =(BottomNavigationView) findViewById(R.id.BottomNavigator);
mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.Nav_First:
Intent First= new Intent(Second.this, First.class);
startActivity(First);
break;
case R.id.Nav_Third:
Intent Third= new Intent(Second.this, Third.class);
startActivity(Third);
break;
}
return true;
}
});
}}
第三项活动是:
底部导航视图 mBottomNavigation;
mBottomNavigation =(BottomNavigationView) findViewById(R.id.BottomNavigator);
mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.Nav_First:
Intent First= new Intent(Third.this, First.class);
startActivity(First);
break;
case R.id.Nav_Second:
Intent Second= new Intent(Third.this, Second.class);
startActivity(Second);
break;
}
return true;
}
});
}}
xml 对于 3 个活动是相同的:
<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BottomNavigator"
android:background="@color/colorPrimaryDark"
android:layout_alignParentBottom="true"
app:itemTextColor="@drawable/item_bg"
app:itemIconTint="@drawable/item_bg"
app:menu="@menu/navigate_items">
</android.support.design.widget.BottomNavigationView>