android 实现 导航效果

在这里插入图片描述

在这里插入图片描述


<?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();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值