Android底部导航组件BottomNavigationView怎么使用

本篇内容介绍了“Android底部导航组件BottomNavigationView怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

BottomNavigationView的简单用法

需求:如上图所示。点击测试一菜单,展示test1fragment。点击测试二菜单,展示test2fragment。点击测试三菜单,展示test3fragment。

第一步,testActivity布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  // 容器,承载fragment
  <FrameLayout
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />
  // BottomNavigationView
  <com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/windowBackground"
    app:menu="@menu/bottom_nav_menu_test" />
</LinearLayout>

第二步,写BottomNavigationView所需要的菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:id="@+id/navigation_test1"
    android:icon="@drawable/ic_home_black_24dp"
    android:title="测试一" />
  <item
    android:id="@+id/navigation_test2"
    android:icon="@drawable/ic_dashboard_black_24dp"
    android:title="测试二" />
  <item
    android:id="@+id/navigation_test3"
    android:icon="@drawable/ic_notifications_black_24dp"
    android:title="测试三" />
</menu>

第三步,书写testActivity文件。重点是setOnNavigationItemSelectedListener点击事件

public class TestActivity extends AppCompatActivity {
  List<Fragment> mFragments = new ArrayList<>();
  test1Fragment t1f = new test1Fragment();
  test2Fragment t2f = new test2Fragment();
  test3Fragment t3f = new test3Fragment();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    BottomNavigationView navView = findViewById(R.id.nav_view);
    mFragments.add(t1f);
    mFragments.add(t2f);
    mFragments.add(t3f);
    // navView 点击事件
    navView.setOnNavigationItemSelectedListener((item)->{
      switchFragment(item.getItemId());
      return true;
    });
  }
  private void switchFragment(int id) {
    Fragment fragment = null;
    switch (id) {
      case R.id.navigation_test1:
        fragment = mFragments.get(0);
        break;
      case R.id.navigation_test2:
        fragment = mFragments.get(1);
        break;
      case R.id.navigation_test3:
        fragment = mFragments.get(2);
        break;
      default:
        break;
    }
    if (fragment != null) {
      getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,fragment).commit();
    }
  }
}

“Android底部导航组件BottomNavigationView怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注蜗牛博客网站,小编将为大家输出更多高质量的实用文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接