BottomNavigationView background transparent

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 BottomNavigationViewthe background , and the background to be the default color when it is the other Fragment.

problem analysis

First try to modify the attribute value BottomNavigationViewin . If you directly set backgroundthe 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 BottomNavigationViewand its parent class, it is found that the background Drawable is obtained through the method in NavigationBarViewthe 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="@nullmake it BottomnavigationViewtransparent;
then in onCreate()the method , set itbinding.navView.setBackgroundColor(0x33A1A1A1) translucent through ;BottomNavigationView

Guess you like

Origin blog.csdn.net/dpdcsdn/article/details/130492077