What happened to windowContentOverlay in Android A

2019-03-08 14:53发布

问题:

After upgrading my phone to Android 4.3 I noticed the shadow below the actionbar is not showing anymore. In my app I've got a custom shadow using windowContentOverlay:

<item name="android:windowContentOverlay">@drawable/shadows_bottom</item>

It's always been showing but now it's gone on API 18. Removing that line from the theme doesn't change anything. while on other API versions it shows a default slight shadow.

Anyone else has noticed that issue?

回答1:

I was able to work around this platform bug by adding the following method to my base FragmentActivity and calling it in onCreate after the layout has been inflated:

/**
 * Set the window content overlay on device's that don't respect the theme
 * attribute.
 */
private void setWindowContentOverlayCompat() {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR2) {
        // Get the content view
        View contentView = findViewById(android.R.id.content);

        // Make sure it's a valid instance of a FrameLayout
        if (contentView instanceof FrameLayout) {
            TypedValue tv = new TypedValue();

            // Get the windowContentOverlay value of the current theme
            if (getTheme().resolveAttribute(
                    android.R.attr.windowContentOverlay, tv, true)) {

                // If it's a valid resource, set it as the foreground drawable
                // for the content view
                if (tv.resourceId != 0) {
                    ((FrameLayout) contentView).setForeground(
                            getResources().getDrawable(tv.resourceId));
                }
            }
        }
    }
}

This works nicely because you don't have to change your themes or dynamically add views to your layouts. It should be forward compatible and can be easily removed once this bug has been fixed.



回答2:

This is officially a bug and will be fixed for next platform release: https://code.google.com/p/android/issues/detail?id=58280

UPDATE: This seems to be fixed on API level 19