admin管理员组

文章数量:1122832

I have an app consisting only of a webview. I'm trying it to update for Android 15 with edge to edge.

Below the current code from layout/webview.xml and MainActivity.kt. It worked well on Android 14.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=";
    xmlns:tools=";
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        actionBar?.hide()
        super.onCreate(savedInstanceState)
        setContentView(layout.webview)
/* code from Empty Views Activity */
        //enableEdgeToEdge()
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

I added the code for edge to edge form an empty project Empty Views Activity generated with New... - New Project....

Now on Android 15, the part of the app extending under the status bar is always white, even if the device has night mode on. The example code seems to work if the ConstraintLayout contains a Textview, but not if it contains a WebView.

How can we fix this?

I have an app consisting only of a webview. I'm trying it to update for Android 15 with edge to edge.

Below the current code from layout/webview.xml and MainActivity.kt. It worked well on Android 14.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        actionBar?.hide()
        super.onCreate(savedInstanceState)
        setContentView(layout.webview)
/* code from Empty Views Activity */
        //enableEdgeToEdge()
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

I added the code for edge to edge form an empty project Empty Views Activity generated with New... - New Project....

Now on Android 15, the part of the app extending under the status bar is always white, even if the device has night mode on. The example code seems to work if the ConstraintLayout contains a Textview, but not if it contains a WebView.

How can we fix this?

Share Improve this question asked Nov 21, 2024 at 9:06 Lorenz MeyerLorenz Meyer 19.9k23 gold badges82 silver badges127 bronze badges 2
  • have you tried android:background=""1234af" for ConstraintLayout? – snachmsm Commented Nov 21, 2024 at 9:19
  • @snachmsm Thanks, this works for setting a fixed background. But what about adapting for night mode? – Lorenz Meyer Commented Nov 21, 2024 at 9:27
Add a comment  | 

1 Answer 1

Reset to default 1

now you need to set android:background for ContraintLayout. if you want to support dark mode then reference to color stored in resources

android:background="@color/status_bar_color"

and set different status_bar_color separately in values/colors.xml and values-night/colors.xml. or you can detect night mode and simply use

val color = if (dark) Color.BLACK else Color.WHITE
findViewById(id.main).setBackgroundColor(color)

I'm also advising to inspect your style set for this Activity, as wrongly configured may lead to white icons on white status bar (or black on black) - look for proper windowLightStatusBar, windowDrawsSystemBarBackgrounds and statusBarColor (which won't work when you target 35+ and won't opt-out with windowOptOutEdgeToEdgeEnforcement)

本文标签: How to layout a webview for Android 15 edge to edgeStack Overflow