environment
Android Studio Dolphin 2021.3.1;
project configuration
compileSdkVersion: 32
androidx.navigation: 2.4.1
Problem Description
The main structure of the project uses a single Activity and multiple Fragments, where the Activity uses FragmentContainerView + BottomNavigationView.
Now the project requires BottomNavigationView
the background , and the background to be the default color when it is the other Fragment.
problem analysis
First try to modify the attribute value BottomNavigationView
in . If you directly set background
the attribute to be transparent, a wireframe with shadow will appear on the background, which is very ugly. The shaded wireframe here should be the default theme or style, which needs to be removed first.
By tracing the implementation of BottomNavigationView
and its parent class, it is found that the background Drawable is obtained through the method in NavigationBarView
the private method createMaterialShapeDrawableBackground()
method the abstract parent class , so consider setting it so that the method returns null.getBackground()
problem solved
First, in the layout file, android:background="@null
make it BottomnavigationView
transparent;
then in onCreate()
the method , set itbinding.navView.setBackgroundColor(0x33A1A1A1)
translucent through ;BottomNavigationView