环境
Android Studio Dolphin 2021.3.1;
项目配置
compileSdkVersion: 32
androidx.navigation: 2.4.1
问题描述
项目主结构使用 单 Activity 多 Fragment, 其中 Activity 使用 FragmentContainerView + BottomNavigationView。
现在项目要求当前 Destination 为第一个 Fragment 时 BottomNavigationView 背景为半透明,是其他 Fragment 时背景为默认颜色。
问题分析
首先尝试修改 Activity 的 layout 文件中 BottomNavigationView 中属性值,如果直接设置 background 属性为透明,背景上会显示出带阴影的线框,非常不美观。这里的阴影线框应该是 theme 或 style 默认的,需要先将其去掉。
通过追溯 BottomNavigationView 及其父类实现,发现在抽象父类 NavigationBarView 中的私有方法 createMaterialShapeDrawableBackground() 方法中通过 getBackground() 方法获取背景 Drawable,因此考虑通过设置,使得该方法返回 null。
问题解决
首先在 layout 文件里面,通过 android:background="@null 使得 BottomnavigationView 为透明;
然后在 Activity 的 onCreate() 方法中,通过 binding.navView.setBackgroundColor(0x33A1A1A1) 给 BottomNavigationView 设置半透明;
文章描述了在AndroidStudioDolphin版本的项目中,如何处理BottomNavigationView在特定Fragment显示半透明背景的问题。作者尝试通过设置layout文件的background为null并自定义Activity的onCreate方法来改变背景颜色,以此消除默认阴影线框并实现所需效果。
161

被折叠的 条评论
为什么被折叠?



