Remove the shadow of BottomNavigationView [effective]

premise

Articles about BottomNavigationViewremoving shadows are basically ineffective on the Internet, such as typical ones Theme="@null", so I researched this problem and decided to write an effective solution post.

Flow-saving assistant: Add attributes in BottomNavigationView: app:elevation="0dp" can remove shadow .



Problem Description

BottomNavigationView, the complete name in the xml file is <com.google.android.material.bottomnavigation.BottomNavigationView />, it looks like this when used (the navigation bar at the bottom):
A regular BottomNavigationView page
If it is BottomNavigationViewplaced in the middle of the screen, the shadow of the entire control will be fully displayed. This shadow effect is actually similar to the cardViewbuilt-in shadow. .
Centered BottomNavigationView


solution

theme="@null"It is invalid if added directly in the control . In fact, the shadow is implemented by the height of the control, that is, the higher the control, the larger the shadow cast. So we can elevationachieve this through this attribute. If you want the shadow to disappear, set the height of the control to 0. The complete writing method is:app:elevation="0dp"

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:elevation="0dp"
    ...
    app:menu="@menu/bottom_nav_menu" />

The effect after setting the elevation attribute to 0dp
If we increase elevationthe value and set it to app:elevation="30dp, then the shadow effect will be larger and more obvious:
The effect after setting the elevation attribute to 30dp
Note : There are two in the control elevation, one is android:elevationand one is app:elevation, the one starting with app is valid , and the one starting with android is invalid . In addition, android:translationZit seems to be adjusting the Z axis, but it actually has no effect on adjusting the shadow.



principle

As you can see from viewing BottomNavigationView, themethere is one set internally elevation, and the value is set to 8dp, so if we want to adjust the shadow, we also set this elevation.

Default style of BottomNavigationView
Default shadow height of BottomNavigationView

Guess you like

Origin blog.csdn.net/Guan_li_peng/article/details/127203993