您现在的位置是:首页 >其他 >BottomNavigationView 背景透明网站首页其他
BottomNavigationView 背景透明
简介BottomNavigationView 背景透明
环境
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 设置半透明;
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。





U8W/U8W-Mini使用与常见问题解决
QT多线程的5种用法,通过使用线程解决UI主界面的耗时操作代码,防止界面卡死。...
stm32使用HAL库配置串口中断收发数据(保姆级教程)
分享几个国内免费的ChatGPT镜像网址(亲测有效)
Allegro16.6差分等长设置及走线总结