premise
Articles about BottomNavigationView
removing 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):
If it is BottomNavigationView
placed in the middle of the screen, the shadow of the entire control will be fully displayed. This shadow effect is actually similar to the cardView
built-in shadow. .
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 elevation
achieve 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" />
If we increase elevation
the value and set it to app:elevation="30dp
, then the shadow effect will be larger and more obvious:
Note : There are two in the control elevation
, one is android:elevation
and one is app:elevation
, the one starting with app is valid , and the one starting with android is invalid . In addition, android:translationZ
it 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
, theme
there 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
.