Android uses BottomNavigationView to achieve the most concise method of bottom navigation (non-wizard generation)

Screenshot of the effect (exactly the same as the one generated by the wizard, the implementation method of responding to the event is different):

 Implementation steps:

1. Add BottomNavigationView in the main XML layout file

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="?android:attr/windowBackground"
    app:labelVisibilityMode="labeled"
    app:itemIconTint="@drawable/bottom_nav_color_selector"
    app:itemTextColor="@drawable/bottom_nav_color_selector"
    app:menu="@menu/bottom_navigation_menu" />

 2. Create the bottom_navigation_menu.xml file in the res/menu folder, and add menu items ( remember to add the corresponding icon resources yourself )

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/title_home" />
    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_dashboard" />
    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_notifications" />
</menu>

3. Set the BottomNavigationView event listener in the Activity

        BottomNavigationView  bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                ActionBar actionBar = getSupportActionBar();
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        // do something
                        actionBar.setTitle("home");
                        return true;
                    case R.id.navigation_dashboard:
                        // do something
                        actionBar.setTitle("dashboard");
                        return true;
                    case R.id.navigation_notifications:
                        // do something
                        actionBar.setTitle("notifications");
                        return true;
                }
                return false;
            }
        });

4. Create a bottom_nav_color_selector.xml file in the res/drawable folder and add a list of color states

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorPrimary" android:state_checked="true" />
    <item android:color="@color/colorGray" />
</selector>

5. In the colors.xml file under the res/values ​​folder, add color definitions

<color name="colorGray">#808080</color>

Or you can quote directly:

<color name="colorGray">@android:color/darker_gray</color>

Guess you like

Origin blog.csdn.net/wh445306/article/details/129839553