最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - enableEdgeToEdge style not working in andorid 15 [AppCompatActivity] - Stack Overflow

programmeradmin1浏览0评论

I'm trying to change the status bar and navigation bar colors using enableEdgeToEdge() in Android 15 (API 34), but the colors are not updating.

Here’s my code inside onCreate() in my MainActivity:



class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Force Light Mode
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // Convert Int to Color
        val primaryDarkColor = Color.RED

        enableEdgeToEdge(
            statusBarStyle = SystemBarStyle.auto(
                lightScrim = primaryDarkColor, // Light mode color
                darkScrim = primaryDarkColor  // Dark mode color
            ),
            navigationBarStyle = SystemBarStyle.auto(
                lightScrim = primaryDarkColor, // Light mode color
                darkScrim = primaryDarkColor  // Dark mode color
            )
        )

        // Adjust window insets
        ViewCompat.setOnApplyWindowInsetsListener(window.decorView.rootView) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(0, systemBars.top, 0, 0)
            insets
        }

       
    }

 
}


I'm trying to change the status bar and navigation bar colors using enableEdgeToEdge() in Android 15 (API 34), but the colors are not updating.

Here’s my code inside onCreate() in my MainActivity:



class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Force Light Mode
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // Convert Int to Color
        val primaryDarkColor = Color.RED

        enableEdgeToEdge(
            statusBarStyle = SystemBarStyle.auto(
                lightScrim = primaryDarkColor, // Light mode color
                darkScrim = primaryDarkColor  // Dark mode color
            ),
            navigationBarStyle = SystemBarStyle.auto(
                lightScrim = primaryDarkColor, // Light mode color
                darkScrim = primaryDarkColor  // Dark mode color
            )
        )

        // Adjust window insets
        ViewCompat.setOnApplyWindowInsetsListener(window.decorView.rootView) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(0, systemBars.top, 0, 0)
            insets
        }

       
    }

 
}


Share Improve this question edited 2 days ago Santhosh Kumar asked Feb 8 at 5:43 Santhosh KumarSanthosh Kumar 5111 silver badge11 bronze badges 2
  • Please edit your post and show where you are calling this code. – BenjyTec Commented Feb 8 at 8:51
  • Hey i edited, check out. – Santhosh Kumar Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 1

According to the official documentation:

If the app targets VANILLA_ICE_CREAM or above, the color will be transparent and cannot be changed.

This means that if your app targets android 15 or higher, the system bars (status bar and navigation bar) will automatically match the background color of your view since edge-to-edge is enforced.

If you only want to change the color of the status bar, you could create a spacer view with full width and no height. Apply WindowInsets as padding to match the status bar height.

please refer to:

https://developer.android.com/reference/android/view/Window#setStatusBarColor(int)

https://developer.android.com/develop/ui/views/layout/edge-to-edge#system_bar_protection

发布评论

评论列表(0)

  1. 暂无评论