当前位置:网站首页>实现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);
边栏推荐
- Lc173. Binary search tree iterator
- Sword finger offer II 099 Sum of minimum paths - double hundred code
- STM32之ADC
- 首批 | 腾讯云完成国内首个云原生安全成熟度评估
- Webrtc audio and video capture and playback examples and mediastream media stream analysis
- Higher order operation of bits
- 设置单击右键可以选择用VS Code打开文件
- 损失函数~
- Jatpack------LiveData
- 容器化技术在嵌入式领域的应用
猜你喜欢

LeetCode 968. Monitor binary tree

从2022年Q1财报看携程的韧性和远景

odoo13搭建医院HRP环境(详细步骤)
![[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?](/img/b5/e746dd115995e82c93f667c58a601c.png)
[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?

`Usage of ${}`

AES高級加密協議的動機闡述

Minimum spanning tree
![[Solved] Splunk: Cannot get username when all users are selected“](/img/13/1e824c8005701e21fc5b4e73308d53.png)
[Solved] Splunk: Cannot get username when all users are selected“

泛型与反射,看这篇就够了

创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋
随机推荐
Local dealers play the community group purchase mode and share millions of operations
Sword finger offer II 099 Sum of minimum paths - double hundred code
AES高级加密协议的动机阐述
中国信通院、清华大学、腾讯安全,云原生安全产学研用强强联合!
[leetcode] most elements [169]
Qt QProgressBar详解
Array advanced improvement
To myself who is about to work
Addition, deletion, modification and query of handwritten ORM (object relationship mapping)
Jerry's built-in shutdown current is 1.2ua, and then it can't be turned on by long pressing [chapter]
【喜欢的诗词】好了歌
[Solved] Splunk: Cannot get username when all users are selected“
antd组件upload上传xlsx文件,并读取文件内容
Splunk audit 的设定
Learning Websites commonly used by circuit designers
Pytorch training CPU usage continues to grow (Bug)
The first batch of Tencent cloud completed the first cloud native security maturity assessment in China
损失函数~
Higher order operation of bits
Construction of Hisilicon 3559 universal platform: draw a frame on the captured YUV image