Pit encountered in night mode

The previous article introduced the implementation of night mode through the Android Support Library, and encountered some pits in the implementation process, and now record the following

1. When setting the color value, accidentally only the color in the night mode is set, but the day mode is not set. The application switches to the night mode and can be accessed normally. The page that calls this color value in the day mode will crash. Report xml can't find this color value, so there is day mode, no night mode, no problem, night mode will read the color value of day mode, otherwise it will crash.

2. Play the video player in full screen, switch to the horizontal screen, and then switch back to the vertical screen. The day mode is normal, but the night mode will cause the original page to go through the life cycle again, although the AndroidManifest.xml is set

android:configChanges="orientation|keyboardHidden|screenSize“

Through research, it is found that the horizontal and vertical screen switching in night mode also triggers UImode, so it needs to be set

android:configChanges="orientation|keyboardHidden|screenSize|uiMode"

3. According to the principle, a drawable selector background is set in the day mode, just set the color value corresponding to the day and night,

However, when some mobile phones switch between day and night modes, there will be confusion in part black and part white, even if recreate is invalid, the reason is that the last cache may be read. The solution is to set a set of drawables for day and night.

In the four night mode, the webview h5 is set with a black background, and the app loading will flash white and then black. The research found that the white background that comes with the webview will be displayed first.

Solution: set webview background transparent

protected void setWebViewNightModel(WebView webView){
    try {
        if (SharedpreferencesUtil.isNightMode(this)) {
            webView.setBackgroundColor(0); // To set the background color xml, you must set the background, otherwise a null pointer will be reported here
            webView.getBackground().setAlpha(0); // Set the fill transparency range: 0-255
        }
    } catch (Exception e) {
        e.printStackTrace ();
    }

}

Five webview set transparent background webView.setBackgroundColor(0);, run crash

Be sure to set the background color of the webview in xml

 

<WebView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/webView"
    android:background="@color/webviewbg"// Be sure to set the background otherwise webView.setBackgroundColor(0); will report a null pointer
    ></WebView>

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325479864&siteId=291194637