如何依赖com.google.android.material:material:1.0.0时无工具类/混淆移除BottomNavigationView动画
刚好之前在维护基于com.google.android.material:material:1.0.0的项目时处理过这个需求,完全不用工具类或ProGuard,给你分享两种直接有效的方式:
方法一:XML配置为主,配合几行代码(最省心)
先在布局文件里给BottomNavigationView加上这些属性,把大部分动画直接关掉:
<com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_nav" android:layout_width="match_parent" android:layout_height="wrap_content" app:menu="@menu/bottom_nav_menu" <!-- 关闭切换标签时的横向平移动画 --> app:itemHorizontalTranslationEnabled="false" <!-- 去掉点击时的涟漪波纹动画 --> app:itemRippleColor="@android:color/transparent" <!-- 让选中/未选中的文字用同一个样式,消除文字缩放动画 --> app:itemTextAppearanceActive="@style/BottomNavTextStyle" app:itemTextAppearanceInactive="@style/BottomNavTextStyle" <!-- 统一图标大小,避免选中时的尺寸波动 --> app:itemIconSize="24dp" />
然后在styles.xml里定义统一的文字样式(按你的需求调整尺寸和颜色):
<style name="BottomNavTextStyle" parent="TextAppearance.MaterialComponents.Caption"> <item name="android:textSize">12sp</item> <item name="android:textColor">@color/bottom_nav_text_selector</item> <!-- 这里的textColor可以用selector区分选中和未选中的颜色 --> </style> <!-- 对应的color selector示例(res/color/bottom_nav_text_selector.xml) --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/primary" android:state_checked="true" /> <item android:color="@color/gray" /> </selector>
最后处理1.0.0版本里没法用XML关闭的图标上移动画,在Activity/Fragment的onCreate里加几行代码:
Kotlin版本:
val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_nav) val menuView = bottomNav.getChildAt(0) as BottomNavigationMenuView // 遍历每个标签项,强制取消上移动画 for (i in 0 until menuView.childCount) { val itemView = menuView.getChildAt(i) as BottomNavigationItemView itemView.translationY = 0f itemView.animate().cancel() // 终止所有正在执行的动画 }
Java版本:
BottomNavigationView bottomNav = findViewById(R.id.bottom_nav); BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNav.getChildAt(0); for (int i = 0; i < menuView.getChildCount(); i++) { BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(i); itemView.setTranslationY(0f); itemView.animate().cancel(); }
方法二:纯代码实现(无需修改XML)
如果不想动布局文件,也可以完全用代码搞定所有动画的关闭:
Kotlin版本:
val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_nav) // 关闭横向平移动画 bottomNav.isItemHorizontalTranslationEnabled = false // 移除点击涟漪效果 bottomNav.itemRippleColor = ColorStateList.valueOf(Color.TRANSPARENT) // 统一文字样式,消除缩放动画 val textStyle = TextAppearanceCompat.create(this, R.style.TextAppearance_MaterialComponents_Caption) bottomNav.itemTextAppearanceActive = textStyle bottomNav.itemTextAppearanceInactive = textStyle // 统一图标大小 bottomNav.itemIconSize = resources.getDimensionPixelSize(R.dimen.bottom_nav_icon_size) // 处理图标上移动画 val menuView = bottomNav.getChildAt(0) as BottomNavigationMenuView for (i in 0 until menuView.childCount) { val itemView = menuView.getChildAt(i) as BottomNavigationItemView itemView.translationY = 0f itemView.animate().cancel() itemView.setStateListAnimator(null) // 彻底禁用状态变化相关的动画 }
关键注意点
- 因为是
1.0.0版本,高版本(比如1.1.0+)新增的itemVerticalTranslationEnabled属性还不存在,所以只能通过代码强制设置translationY来关闭图标上移的动画。 - 如果不需要完全移除点击反馈,可以删掉涟漪效果相关的配置,保留默认的点击动画。
- 统一文字样式是为了消除选中时文字放大、未选中时缩小的渐变动画,让文字大小始终保持一致。
内容的提问来源于stack exchange,提问作者j.elmer
相关产品推荐
相关产品推荐

