0
0

BottomNavigationViewのWindowInsetsを変える

Posted at

Edge-to-Edge 対応で現状のレイアウト構成から仕方なく material-components の BottomNavigationView のデフォルトの WindowInsets から適用する箇所を変更したいときの方法です。


BottomNavigationView のデフォルトの WindowInsets は内部でのみ処理されているのでパラメータでの切り替えができません。

そのため、レイアウトの構成によっては不要な WindowInsets がついてしまう場合があります。

Screenshot_20240627_195656.png

このスクショの場合だと DisplayCutout 分の WindowInsets が余分についてしまっています。


この解決には WindowInsets の消費の仕組みを利用します。
BottomNavigationView に対して ViewCompat.setOnApplyWindowInsetsListenerWindowInsetsCompat.CONSUMED を返して処理したことにしてしまえば、BottomNavigationView 内部の WindowInsets の処理が呼ばれなくなります。

ViewCompat.setOnApplyWindowInsetsListener(bottomNavigationView) { v, windowInsets ->
    WindowInsetsCompat.CONSUMED
}

もちろん WindowInsetsCompat.CONSUMED を返すだけだと必要な WindowInsets まで消えてしまうことになります。

Screenshot_20240627_201135.png

そのため、必要な WindowInsets を設定しつつ WindowInsetsCompat.CONSUMED を返すようにしましょう。

ViewCompat.setOnApplyWindowInsetsListener(bottomNavigationView) { view, windowInsets ->
    val insets = windowInsets.getInsets(WindowInsetsCompat.Type.navigationBars())
    view.updatePadding(
        bottom = insets.bottom
    )
    WindowInsetsCompat.CONSUMED
}

Screenshot_20240627_195742.png

もちろん、この WindowInsets の消費による BottomNavigationView の WindowInsets の上書きは Insetter での実装でも可能です。

0
0
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
0
0