Learning Android development BottomNavigationView

Foreword

  Note that this is in the introduction of com.google.android.material.bottomnavigation.BottomNavigationView AndroidX

xml layout

   <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:menu="@menu/p_home_bottom_menu"
        app:labelVisibilityMode="labeled"
        app:itemTextColor="@color/fontBlack1"
        app:itemTextAppearanceActive="@style/Active"
        app:itemTextAppearanceInactive="@style/Inactive"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

    </com.google.android.material.bottomnavigation.BottomNavigationView>

 

  • App: labelVisibilityMode = "Labeled" label display mode, in fact, is to change the whole animation after click, select the recommended labeled, the default disgusting more than 3 item will be super ugly animation
  • Color itemTextColor = "@ color / fontBlack1" item text: app
  • app: itemTextAppearanceActive = "@ style / Active" setting item is selected after effects
  • app: itemTextAppearanceInactive = "@ style / Inactive" setting is not selected item effect

style

<style name="Active" parent="@style/TextAppearance.AppCompat.Caption">
        <item name="android:textSize">@dimen/font_size_17</item>
    </style>

    <style name="Inactive" parent="@style/TextAppearance.AppCompat.Caption">
        <item name="android:textSize">@dimen/font_size_11</item>
    </style>

Just change the text size

menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/home"
        android:title="首页"
        android:icon="@mipmap/ic_admission"/>

    <item
        android:id="@+id/notice"
        android:title="通知"
        android:icon="@mipmap/ic_head"/>

    <item
        android:id="@+id/circle"
        android:title="圈子"
        android:icon="@mipmap/ic_temp"/>

    <item
        android:id="@+id/my_info"
        android:title="我的"
        android:icon="@mipmap/ic_notice"/>

</menu>

Tint color icon is covered

You will find the icon to add Tint color icon will be grayed out icon covered, the following two lines of code to solve this problem

        mBottomNavigationView = findViewById(R.id.bottom_navigation_view);
        mBottomNavigationView.setItemIconTintList(null);

 

If you need to change the selected icon

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/ic_home_page_normal"/>
    <item android:state_checked="true" android:drawable="@drawable/ic_home_page_selected"/>
</selector>

Calls on the menu item

<item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/title_home" />

 Two click listener

mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                Log.e("ytzn", "onNavigationItemSelected: 选中"+menuItem.getItemId() );
                return true;
            }
        });
        mBottomNavigationView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
            @Override
            public void onNavigationItemReselected(@NonNull MenuItem menuItem) {
                Log.e ( "ytzn", "onNavigationItemSelected: selected again to select" + menuItem.getItemId ());

            }
        });
setOnNavigationItemSelectedListener callback is not selected after clicking the item, return the boolean is to decide whether to enable the selected effect or zoom effect
setOnNavigationItemReselectedListener if already selected, the callback after clicking once




end

Guess you like

Origin www.cnblogs.com/guanxinjing/p/11027145.html