概要
Androidで下タブとして使えるBottomNavigationですが、各Itemのタイトルが長すぎたりすると末尾がカットされて全体が表示されなかったりします。基本的にはタイトルを適切な長さにすべきですが、どうしても長めのタイトル全体を表示したいといった時、下記のようにBottomNavigationItemViewのレイアウトにアクセスして変更を加えることが可能です。
例
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fixBottomNavigationViewLayout(findViewById(R.id.bottom_navigation))
}
}
private fun fixBottomNavigationViewLayout(view: BottomNavigationView) {
try {
val menuView = view.getChildAt(0) as BottomNavigationMenuView
// BottomNavigationの各Itemにアクセス
for (i in 0 until menuView.childCount) {
val itemView = menuView.getChildAt(i) as BottomNavigationItemView
// smallLabel(非選択時のTextView)を取得し、文字サイズを変更
itemView.findViewById<TextView>(R.id.smallLabel).apply {
textSize = 10F
}
// largeLabel(選択時のTextView)を取得し、Paddingや文字サイズ等を変更
itemView.findViewById<TextView>(R.id.largeLabel).apply {
setPadding(0, 0, 0, 0)
ellipsize = TextUtils.TruncateAt.END
textSize = 10F
}
}
} catch (e: Exception) {
Logger.e(e.localizedMessage)
}
}
参考
https://github.com/material-components/material-components-android/issues/139
https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/bottomnavigation/res/layout/design_bottom_navigation_item.xml#L45