如何设置BottomNavigationView选中项文本为常规样式而非粗体
你的BottomNavigationView默认会在选中项时将文本设为粗体,要改成常规样式,可按以下方法操作:
方法一:通过XML属性直接设置(推荐)
- 在
res/values/styles.xml中定义一个使用常规字体的文本样式:
<style name="BottomNavNormalText" parent="TextAppearance.MaterialComponents.Caption"> <item name="android:textStyle">normal</item> </style>
- 在你的BottomNavigationView布局中添加
app:itemTextAppearanceActive属性,指向刚才定义的样式:
<com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/navView" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/backgroundColor1" android:elevation="8dp" app:itemIconTint="@color/bottom_nav_color" app:itemTextColor="@color/bottom_nav_color" app:itemTextAppearanceActive="@style/BottomNavNormalText" <!-- 新增属性 --> app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/bottom_nav_menu" />
方法二:通过代码动态设置
如果需要更灵活的控制,也可以在代码中遍历菜单项,强制设置选中时的文本样式:
val bottomNav = findViewById<BottomNavigationView>(R.id.navView) for (i in 0 until bottomNav.menu.size()) { val textView = bottomNav.findViewById<TextView>(com.google.android.material.R.id.design_menu_item_text) textView?.setTextStyle(Typeface.NORMAL) }
你的原颜色选择器代码bottom_nav_color.xml无需修改,保持原有逻辑即可:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:color="@color/itemCheckedColor" /> <item android:state_checked="false" android:color="@color/itemNotCheckedColor" /> </selector>
内容的提问来源于stack exchange,提问作者Kolovrat
相关产品推荐
相关产品推荐

