当前位置:网站首页>实现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);
边栏推荐
- Jerry's prototype has no touch, and the reinstallation becomes normal after dismantling [chapter]
- 严守工期,确保质量,这家AI数据标注公司做到了!
- Chow-Liu Tree
- Distributed monitoring system ZABBIX
- STM32串口DAM接收253字节就死机原因排查
- Splunk audit setting
- Niuke network: maximum submatrix
- Methods to solve the tampering of Chrome browser and edeg browser homepage
- Lambda expression: an article takes you through
- 首批 | 腾讯云完成国内首个云原生安全成熟度评估
猜你喜欢
P1007 独木桥
中国信通院、清华大学、腾讯安全,云原生安全产学研用强强联合!
[leetcode] most elements [169]
[Yangcheng cup 2020] easyphp
The kth largest element in the [leetcode] array [215]
损失函数~
Xiaopeng P7 had an accident and the airbag did not pop up. Is this normal?
【板栗糖GIS】arcmap—如何批量修改注记要素的字体,颜色,大小等
Set right click to select vs code to open the file
Loss function~
随机推荐
LeetCode 968. Monitor binary tree
海思3559万能平台搭建:在截获的YUV图像上旋转操作
Introduction and response to high concurrency
psnr,ssim,rmse三个指标的定量分析
Minimum spanning tree
STM32之ADC
WebRTC音视频采集和播放示例及MediaStream媒体流解析
[Solved] Splunk: Cannot get username when all users are selected“
Gas station [problem analysis - > problem conversion - > greed]
Start from the bottom structure to learn the customization and testing of FPGA --- Xilinx ROM IP
Pytorch training CPU usage continues to grow (Bug)
Jerry's charge unplugged, unable to touch the boot [chapter]
Brief introduction of emotional dialogue recognition and generation
Successfully changed Splunk default URL root path
分布式监控系统zabbix
设置单击右键可以选择用VS Code打开文件
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月1日08:43:06
[chestnut sugar GIS] ArcScene - how to make elevation map with height
mysql重置密码,忘记密码,重置root密码,重置mysql密码
Introduction to the latest plan of horizon in April 2022