Android底部导航组件BottomNavigationView
作者:Dormiveglia-flx 时间:2022-12-02 12:48:05
什么是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();
}
}
}
来源:https://feilx.blog.csdn.net/article/details/129489371
标签:Android,底部导航,BottomNavigationView
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android ActionBar使用教程
2023-10-24 19:09:08
在mybatis中使用mapper进行if条件判断
2023-08-01 08:09:34
C#可用于登录验证码的四位随机数生成方法
2021-08-01 00:31:01
android使用viewpager计算偏移量实现选项卡功能
2023-12-06 12:53:02
![](https://img.aspxhome.com/file/2023/9/123339_0s.png)
Android使用AutoCompleteTextView实现自动填充功能的案例
2023-03-26 06:56:47
![](https://img.aspxhome.com/file/2023/3/139193_0s.png)
C#下载歌词文件的同步和异步方法
2023-04-11 22:46:49
![](https://img.aspxhome.com/file/2023/4/77304_0s.jpg)
WinForm中实现picturebox自适应图片大小的方法
2022-09-07 21:14:01
Mybatis延迟加载的实现方式
2023-08-19 11:07:32
![](https://img.aspxhome.com/file/2023/8/67018_0s.png)
Java并发之线程池Executor框架的深入理解
2022-03-13 10:20:55
![](https://img.aspxhome.com/file/2023/5/109945_0s.png)
Kotlin开发Android应用实例详解
2023-09-11 00:22:50
![](https://img.aspxhome.com/file/2023/9/58149_0s.png)
C#实现FFT(递归法)的示例代码
2022-12-30 05:21:06
![](https://img.aspxhome.com/file/2023/1/116611_0s.png)
maven assembly打包生成Java应用启动脚本bat和sh的方法
2023-09-16 03:06:54
![](https://img.aspxhome.com/file/2023/6/108936_0s.jpg)
c# List find()方法返回值的问题说明(返回结果为对象的指针)
2023-11-20 21:55:34
![](https://img.aspxhome.com/file/2023/7/74817_0s.png)
Java冒泡排序及优化介绍
2023-11-11 13:05:51
解决Druid动态数据源配置重复刷错误日志的问题
2021-06-06 17:44:51
![](https://img.aspxhome.com/file/2023/4/104024_0s.jpg)
java基于UDP实现在线聊天功能
2021-06-08 00:01:44
![](https://img.aspxhome.com/file/2023/3/89603_0s.jpg)
浅谈Java slf4j日志简单理解
2021-07-07 15:49:15
java io读取文件操作代码实例
2023-04-12 08:53:57
Java序列化与反序列化的实例分析讲解
2022-09-16 05:58:39
Java实现AOP面向切面编程的实例教程
2023-02-20 19:32:38