当前位置:网站首页>实现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);
边栏推荐
- go 多线程数据搜索
- [leetcode] reverse the word III in the string [557]
- boot actuator - prometheus使用
- Antd component upload uploads xlsx files and reads the contents of the files
- Array advanced improvement
- 【板栗糖GIS】arcmap—为什么使用自定义捕捉的时候,经典捕捉的勾要去掉呢?
- Local dealers play the community group purchase mode and share millions of operations
- 20220524_数据库过程_语句留档
- [chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?
- Sword finger offer II 099 Sum of minimum paths - double hundred code
猜你喜欢
随机推荐
Methods to solve the tampering of Chrome browser and edeg browser homepage
【板栗糖GIS】arcmap—为什么使用自定义捕捉的时候,经典捕捉的勾要去掉呢?
Data analysis learning records -- complete a simple one-way ANOVA with Excel
MySQL queries nearby data And sort by distance
Xiaopeng P7 had an accident and the airbag did not pop up. Is this normal?
JS syntax ES6, ES7, es8, es9, ES10, es11, ES12 new features (Abstract)
ServletContext learning diary 1
Jerry's charge unplugged, unable to touch the boot [chapter]
Jerry's prototype has no touch, and the reinstallation becomes normal after dismantling [chapter]
Loss function~
成功改变splunk 默认URL root path
xshell配置xforward转发火狐浏览器
[leetcode] there are duplicate elements [217]
Go语言sqlx库操作SQLite3数据库增删改查
[leetcode] most elements [169]
Antd component upload uploads xlsx files and reads the contents of the files
[hardware] origin of standard resistance value
归并排序详解及应用
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
深度剖析数据在内存中的存储----C语言篇