Letting WebView on Android work with prefers-color-scheme: dark

前端 未结 4 1639
余生分开走
余生分开走 2020-12-29 23:20

I have an Android App that uses webview, and lately I\'m trying to figure out how to add a dark theme by using the new @media (prefers-color-scheme: dark) CSS s

4条回答
  •  鱼传尺愫
    2020-12-29 23:59

    What I do, based on your question code, is to force based on current status:

    int nightModeFlags = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
    if (nightModeFlags == Configuration.UI_MODE_NIGHT_YES) {
      webSettings.setForceDark(WebSettings.FORCE_DARK_ON);
    }
    

    This way @media (prefers-color-scheme: dark) works, but @media (prefers-color-scheme: light) still doesn't work (tried using FORCE_DARK_OFF and FORCE_DARK_AUTO in an else)

提交回复
热议问题