我想在完成底部导航后为应用程序构建底部导航。布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.BottomNavigationView
android:id="@+id/Bottomnav"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_gravity="bottom"
android:background="#FFFFFF"
app:menu="@menu/bottomnav_menus">
</android.support.design.widget.BottomNavigationView>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
接下来,我进入了实现onnavigationitemselectlistner的主要活动,但是在完成此方法之后,我没有出现任何错误。不起作用。
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.live:
Intent intent = new Intent(MainActivity.this, LiveScore.class);
startActivity(intent);
return true;
}
return false;
}
});
我的菜单项:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title=""
android:id="@+id/home"
android:icon="@drawable/baseline_home_black_24dp"
android:orderInCategory="1"/>
<item
android:title=""
android:id="@+id/live"
android:icon="@drawable/baseline_live_tv_black_24dp"
android:orderInCategory="2"/>
<item
android:title=""
android:id="@+id/video"
android:icon="@drawable/baseline_video_library_black_24dp"
android:orderInCategory="3"/>
</menu>
请评论是否需要其他信息。
答案 0 :(得分:0)
替换此
switch (item.getItemId())
{
case R.id.live:
Intent intent = new Intent(MainActivity.this, LiveScore.class);
startActivity(intent);
return true;
}
return false;
通过
switch (item.getItemId())
{
case R.id.live:
Intent intent = new Intent(MainActivity.this,LiveScore.class);
startActivity(intent);
break;
}
return true;
答案 1 :(得分:0)
尝试以下方法,对我来说很好
菜单xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_home"
android:icon="@drawable/ic_home_blue_48dp"
android:title="Home" />
<item
android:id="@+id/action_menu"
android:icon="@drawable/ic_apps_black_24dp"
android:title="Menu"
/>
<item
android:id="@+id/action_msg"
android:icon="@drawable/ic_chat_black_24dp"
android:title="Message Inbox"
/>
</menu>
xml
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
app:itemIconTint="@drawable/bottom_nav_colors"
app:itemTextColor="@drawable/bottom_nav_colors"
app:menu="@menu/bottom_navigation_items"/>
底部侦听器
BottomNavigationView bottomNavigationView;
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
{
Fragment selectedFragment = null;
switch (item.getItemId())
{
case R.id.action_home:
//perform action
break;
case R.id.action_menu:
//perform action
break;
case R.id.action_msg:
//perform action
break;
}
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, selectedFragment);
transaction.commit();
return true;
}
});
答案 2 :(得分:0)
确保您要添加以下代码
navView.setOnNavigationItemSelectedListener(new ...);
navView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
@Override
public void onNavigationItemReselected(@NonNull MenuItem item) {
Toast.makeText(MainActivity.this, "Reselected", Toast.LENGTH_SHORT).show();
}
});
之后
NavigationUI.setupWithNavController(navView, navController);
这个