检查导航栏上的多个项目



我有一个包含 4 个项目的底部导航栏,我希望多个项目可检查。我知道此栏用于浏览活动,但我希望chackable1 个以上的项目

如果不可检查,请至少单独更改项目的文本颜色或图标颜色,而不是将它们作为一个整体进行更改

法典

final BottomNavigationView bottomNavigation = findViewById(R.id.bottom_navigation);
bottomNavigation.getMenu().setGroupCheckable(0, true, true);
BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.vpn:
bottomNavigation.getMenu().findItem(R.id.home).setVisible(false);
bottomNavigation.getMenu().findItem(R.id.home).setChecked(true);
bottomNavigation.getMenu().findItem(R.id.theme).setTitle("ON");
Toast.makeText(getApplicationContext(),"VPN",Toast.LENGTH_SHORT).show();
return true;
case R.id.incognito:
Toast.makeText(getApplicationContext(),"Private",Toast.LENGTH_SHORT).show();
return true;
case R.id.theme:
Toast.makeText(getApplicationContext(),"Theme",Toast.LENGTH_SHORT).show();
return true;
case R.id.home:
Toast.makeText(getApplicationContext(),"Home",Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
};
bottomNavigation.setOnNavigationItemSelectedListener(navigationItemSelectedListener);

XML代码

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/black"
android:theme="@style/BottomNavigationTheme"
app:labelVisibilityMode="labeled"
app:menu="@menu/bottom_navigation_menu"/>

风格.xml

<style name="BottomNavigationTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/white</item>
<item name="android:textColorSecondary">@color/white</item>
</style>

您可以使用以下代码进行设置 -

bottomNavigation.setSelectedItemId(R.id.my_menu_item_id1);
bottomNavigation.setSelectedItemId(R.id.my_menu_item_id2);
bottomNavigation.setSelectedItemId(R.id.my_menu_item_id3);

或者尝试下面的代码来选择多个项目 -

bottomNavigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
for (int i = 0; i < 5; i++) { //here 5 is item count
MenuItem top = navigationView.getMenu().getItem(0).getSubMenu().getItem(i);
if (top.isChecked()) top.setChecked(false);
}
return true;
}
});

相关内容

  • 没有找到相关文章

最新更新