5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AndroidでStatusBarとNavigationBarを透過させつつ全画面表示する

Posted at

はじめに

ActionBar消してStatusBarとNavigationBarを透過させて全画面表示する方法です。
完成形は以下の通り。

透過させる前 透過させた後

消していきます

Barを消したいActivityに以下を記述

MainActivity.kt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.hide() // ActionBarを消す
        hideSystemBars()
        setContentView(R.layout.activity_main)
    }

    // StatusBarとNavigationBarの範囲まで表示できるようにする
    private fun hideSystemBars() {
        // Android11とそれ未満で処理を分ける
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window.setDecorFitsSystemWindows(false)
            window.isNavigationBarContrastEnforced = false
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
        }
    }
}

さらに消したいActivityに適用しているthemeでStatusBarとNavigationBarを透過させます。

themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.TransparentSystemBar" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        ...
        <!-- StatuBarとNavigationBarの色を透過させる -->
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:navigationBarColor">@android:color/transparent</item>
    </style>
</resources>

以上です。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?