广告
返回顶部
首页 > 资讯 > 移动开发 >Android底部导航组件BottomNavigationView
  • 519
分享到

Android底部导航组件BottomNavigationView

Android底部导航组件AndroidBottomNavigationView 2023-03-20 11:03:02 519人浏览 八月长安
摘要

目录什么是BottomNavigationViewBottomNavigationView的简单用法什么是BottomNavigationView 底部菜单栏 BottomNavi

什么是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的文章就介绍到这了,更多相关Android BottomNavigationView内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Android底部导航组件BottomNavigationView

本文链接: https://www.lsjlt.com/news/200538.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作