[Android Studio] Implementation of the bottom navigation bar

1. Create a new project

Create a new project, select "Bottom Navigation Activity", and click "Next".

 2、activity_main.xml

The interface layout is: BottomNavigationView+fragment.

In BottomNavigationView, app:menu: bottom navigation bar button menu.

In the fragment, app:navGraph: associates the navigation graph.

<?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"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />//底部的导航按钮

    <fragment
        android:id="@+id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />//导航图

</androidx.constraintlayout.widget.ConstraintLayout>

3. bottom_nav_menu.xml menu file

Define the navigation buttons at the bottom in this file, and each item represents a navigation button. Up to five navigation buttons can be displayed. At the beginning, the system creates 3 navigation buttons by default, and you can add them according to your needs.

illustrate:

  1. android:icon<the icon displayed by the navigation button>
  2. android:title<the text displayed by the navigation button>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@mipmap/bottom_btn1"
        android:title="账单" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@mipmap/bottom_btn2"
        android:title="图表" />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@mipmap/bottom_btn3"
        android:title="记账" />
    <item
        android:id="@+id/navigation_blank"
        android:icon="@mipmap/bottom_btn5"
        android:title="我的" />

</menu>

4、mobile_navigation.xml

The role of this file is to define fragment.

illustrate:

  1. The id of each fragment must be consistent with the corresponding bottom navigation button id
  2. In navigation: app:startDestination<indicates the default fragment to start>
  3. In fragment: android:name<and its corresponding Java class>
  4. In the fragment: android:label<text displayed on the title bar at the top of the interface>
  5. In the fragment: tools:layout<specify the layout file>
<?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/mobile_navigation"
    app:startDestination="@+id/navigation_home"> //开始默认显示的fragment

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.example.ji_zhang_ben.ui.home.HomeFragment" 
        android:label="账单"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.example.ji_zhang_ben.ui.dashboard.DashboardFragment"
        android:label="图表"
        tools:layout="@layout/fragment_dashboard" />

    <fragment
        android:id="@+id/navigation_notifications"
        android:name="com.example.ji_zhang_ben.ui.notifications.NotificationsFragment"
        android:label="记账"
        tools:layout="@layout/fragment_notifications" />

    <fragment
        android:id="@+id/navigation_blank"
        android:name="com.example.ji_zhang_ben.ui.blank.BlankFragment"
        android:label="我的"
        tools:layout="@layout/fragment_blank" />
</navigation>

5、MainActivity.java

package com.example.ji_zhang_ben; 

import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.ji_zhang_ben.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);

        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications,R.id.navigation_blank)
                .build();

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);
    }

}

6、HomeFragment.java

The corresponding Java class codes of the four Fragments are the same, and only one of them is given as an example.

package com.example.ji_zhang_ben.ui.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.example.ji_zhang_ben.R;


public class HomeFragment extends Fragment {

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_home, container, false);
        return view;                   //相应的布局文件
    }

}

7. Project structure diagram

 8. Realize the effect

 9. Picture material

Guess you like

Origin blog.csdn.net/zhou_ge1/article/details/125543005