当前位置:网站首页>实现BottomNavigationView和Navigation联动
实现BottomNavigationView和Navigation联动
2022-07-02 22:09:00 【自动2004郝金辉】
实现BottomNavigationView和Navigation联动
界面展示
引入依赖
// Navigation
implementation "androidx.navigation:navigation-fragment:2.2.2"
implementation "androidx.navigation:navigation-ui:2.2.2"
创建Fragment
创建navigation
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/navigation" app:startDestination="@id/oneFragment2">
<fragment android:id="@+id/oneFragment2" android:name="com.example.navigation.OneFragment" android:label="fragment_one" tools:layout="@layout/fragment_one" />
<fragment android:id="@+id/twoFragment2" android:name="com.example.navigation.TwoFragment" android:label="fragment_two" tools:layout="@layout/fragment_two" />
<fragment android:id="@+id/threeFragment2" android:name="com.example.navigation.ThreeFragment" android:label="fragment_three" tools:layout="@layout/fragment_three" />
</navigation>
注意这里的三个ID
为BottomNavigationView创建menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/oneFragment2" android:icon="@drawable/ic_launcher_background" android:title="Item" />
<item android:id="@+id/twoFragment2" android:icon="@drawable/ic_launcher_background" android:title="Item" />
<item android:id="@+id/threeFragment2" android:icon="@drawable/ic_launcher_background" android:title="Item" />
</menu>
这里的三个ID必须和刚才一样
主界面布局
<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" >
<androidx.fragment.app.FragmentContainerView android:id="@+id/fragmentContainerView" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" app:defaultNavHost="true" app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/main_bottom_navigation_view" android:layout_width="match_parent" android:layout_height="50dp" app:menu="@menu/menu" />
</LinearLayout>
实现BottomNavigationView和Navigation联动
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragmentContainerView);
NavController navController = navHostFragment.getNavController();
BottomNavigationView bottomNavigationView = findViewById(R.id.main_bottom_navigation_view);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
}
}
主要是三面四行代码
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragmentContainerView);
NavController navController = navHostFragment.getNavController();
BottomNavigationView bottomNavigationView = findViewById(R.id.main_bottom_navigation_view);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
边栏推荐
- Application of containerization technology in embedded field
- 【喜欢的诗词】好了歌
- Comprehensively analyze the logic of the shared purchase business model? How sharing purchase empowers Enterprises
- 【板栗糖GIS】arcmap—如何批量修改注记要素的字体,颜色,大小等
- Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月1日08:43:06
- MySQL查询附近的数据.并按距离进行排序.
- go 多线程数据搜索
- mysql重置密码,忘记密码,重置root密码,重置mysql密码
- [leetcode] reverse string [344]
- 力扣刷题(2022-6-28)
猜你喜欢
[羊城杯2020]easyphp
Qt QProgressBar详解
Looking at Ctrip's toughness and vision from the Q1 financial report in 2022
【板栗糖GIS】global mapper 如何通过dsm批量制作贴地等高线
[chestnut sugar GIS] how does global mapper batch produce ground contour lines through DSM
成功改变splunk 默认URL root path
[npuctf2020]ezlogin XPath injection
Array advanced improvement
The first batch of Tencent cloud completed the first cloud native security maturity assessment in China
Addition, deletion, modification and query of handwritten ORM (object relationship mapping)
随机推荐
[Solved] Splunk: Cannot get username when all users are selected“
损失函数~
Jerry's charge unplugged, unable to touch the boot [chapter]
odoo13搭建医院HRP环境(详细步骤)
設置單擊右鍵可以選擇用VS Code打開文件
go 4種單例模式
Qt QProgressBar详解
Uniapp wechat login returns user name and Avatar
【板栗糖GIS】arcmap—如何批量修改注记要素的字体,颜色,大小等
首批 | 腾讯云完成国内首个云原生安全成熟度评估
静态文件显示问题
力扣刷题(2022-6-28)
Detailed explanation and application of merging and sorting
[chestnut sugar GIS] how does global mapper batch produce ground contour lines through DSM
Odoo13 build a hospital HRP environment (detailed steps)
Go语言sqlx库操作SQLite3数据库增删改查
Lambda expression: an article takes you through
成功改变splunk 默认URL root path
Webrtc audio and video capture and playback examples and mediastream media stream analysis
[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?