应该怎么改
时间: 2023-11-29 09:05:38 浏览: 32
如果您想采用导航栏作为应用程序的导航方式,可以考虑将代码改成以下形式:
```
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 页面初始化导航栏
initNavigationBar();
}
private void initNavigationBar() {
BottomNavigationView navigationView = findViewById(R.id.bottom_navigation);
navigationView.setOnNavigationItemSelectedListener(item -> {
switch (item.getItemId()) {
case R.id.navigation_home:
// 跳转到首页
Intent homeIntent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(homeIntent);
return true;
case R.id.navigation_anli:
// 跳转到案例页面
Intent anliIntent = new Intent(MainActivity.this, AnliActivity.class);
startActivity(anliIntent);
return true;
case R.id.navigation_shejishi:
// 跳转到设计师页面
Intent shejishiIntent = new Intent(MainActivity.this, ShejishiActivity.class);
startActivity(shejishiIntent);
return true;
}
return false;
});
}
```
上述代码中,我们使用了 BottomNavigationView 作为导航栏,并在其中添加了三个菜单项,分别对应着首页、案例和设计师页面。通过设置 OnNavigationItemSelectedListener 监听器来处理菜单项的点击事件,从而实现页面之间的跳转。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)