Android Kotlin底部导航选中项图标高亮无法禁用求助
方案1:XML布局直接配置
在布局文件的BottomNavigationView标签里添加以下属性,直接移除选中时的背景高亮和点击波纹:
app:itemBackground="@android:color/transparent" app:itemRippleColor="@android:color/transparent"
itemBackground设为透明:去掉选中项图标周围的高亮背景itemRippleColor设为透明:移除点击时的波纹反馈(不需要的话可设置,按需选择)
方案2:Kotlin代码动态设置
如果需要在代码里调整,可添加这段代码:
// 假设你的BottomNavigationView实例是bottomNav bottomNav.itemBackground = ColorDrawable(Color.TRANSPARENT) bottomNav.itemRippleColor = ColorStateList.valueOf(Color.TRANSPARENT)
方案3:自定义全局样式
如果多个底部导航都需要这个效果,可在styles.xml中定义统一样式:
<style name="NoHighlightBottomNav" parent="Widget.MaterialComponents.BottomNavigationView"> <item name="itemBackground">@android:color/transparent</item> <item name="itemRippleColor">@android:color/transparent</item> </style>
然后在布局中引用该样式:
<com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottomNav" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/NoHighlightBottomNav" ... />
内容的提问来源于stack exchange,提问作者Ahmad Elahi
相关产品推荐
相关产品推荐

