5

我想在完成我的底部导航后为我的应用程序构建带有活动的底部导航。布局。

<?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>

如果您需要任何其他信息,请发表评论。

4

3 回答 3

6

确保您添加以下代码

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);  

这个

于 2020-10-05T12:32:04.203 回答
2

试试下面,它对我来说很好用

菜单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;
        }
    });
于 2019-01-15T04:46:57.943 回答
1

替换这个

  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;
于 2019-01-14T15:04:55.503 回答