LoginSignup
1
1

More than 5 years have passed since last update.

[Android] BottomNavigationViewのレイアウトをいじる

Last updated at Posted at 2019-01-07

概要

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

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1