<?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/my_navgation"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.example.myapplication.HomeFragment"
android:label="home_fragment"
tools:layout="@layout/home_fragment"/>
<fragment
android:id="@+id/typeFragment"
android:name="com.example.myapplication.TypeFragment"
android:label="type_fragment"
tools:layout="@layout/type_fragment"/>
<fragment
android:id="@+id/aboutFragment"
android:name="com.example.myapplication.AboutFragment"
android:label="about_fragment"
tools:layout="@layout/about_fragment"/>
</navigation>
定义 menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/firstFragment"
android:icon="@drawable/ic_looks_one_black_24dp"
android:title="旋转" />
<item
android:id="@+id/secondFragment"
android:icon="@drawable/ic_looks_two_black_24dp"
android:title="缩放" />
<item
android:id="@+id/thirdFragment"
android:icon="@drawable/ic_looks_3_black_24dp"
android:title="移动" />
</menu>
定义 activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/my_navgation"
app:defaultNavHost="true"
android:id="@+id/fragment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.0"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:itemBackground="@color/colorWhite"
app:menu="@menu/app_menu"
android:id="@+id/bottomNavigationView"/>
</androidx.constraintlayout.widget.ConstraintLayout>
使用一个 navigationHost 组件,绑定 navigation 逻辑
java 代码配置
package com.example.myapplication;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.example.myapplication.core.BaseAnnotationActivity;
import com.example.myapplication.core.annon.BindView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
/**
* @Author lyr
* @create 2020/9/24 14:02
*/
@BindView(view = R.layout.app_content)
public class ContentActivity extends BaseAnnotationActivity {
/**
* 自定义回调
*/
@Override
protected void onCreateCustom() {
//获取导航条组件
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
//获取 路由控制器
NavController navController = Navigation
.findNavController(this, R.id.fragment);
//设置 路由跳转逻辑配置
AppBarConfiguration configuration = new AppBarConfiguration
.Builder(navController.getGraph()).build();
//装配
NavigationUI.setupActionBarWithNavController(this, navController);
NavigationUI.setupWithNavController(bottomNavigationView,navController);
}
@Override
public boolean onSupportNavigateUp() {
NavController controller = Navigation.findNavController(this, R.id.fragment);
return controller.navigateUp();
}
}