【问题标题】:What happened to windowContentOverlay in Android API 18?Android API 18 中的 windowContentOverlay 发生了什么?
【发布时间】:2013-07-30 12:38:31
【问题描述】:

将手机升级到 Android 4.3 后,我注意到操作栏下方的阴影不再显示。在我的应用中,我使用windowContentOverlay 获得了自定义阴影:

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

它一直在显示,但现在它已在 API 18 上消失。 从主题中删除该行不会改变任何内容。而在其他 API 版本上,它会显示默认的轻微阴影。

其他人注意到这个问题吗?

【问题讨论】:

  • 我今天在我们的应用程序中注意到了同样的事情。希望有一个简单的解决方案。
  • 你在使用 ActionBarSherlock 吗?
  • 看起来 ActionBarSherlock 人员提供了有关此更改的更多详细信息:github.com/JakeWharton/ActionBarSherlock/issues/1003
  • 如果您仔细观察,现在几个第一方应用程序中都缺少阴影。事实上,在 DDMS 中转储视图层次结构表明,显示 windowContentOverlay 可绘制对象的 ImageView 根本不存在于新的 ActionBar 布局中。
  • 这似乎已从 API 级别 19 修复。

标签: android android-actionbar android-4.3-jelly-bean


【解决方案1】:

我能够通过将以下方法添加到我的基础 FragmentActivity 并在布局膨胀后在 onCreate 中调用它来解决这个平台错误:

/**
 * 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));
                }
            }
        }
    }
}

这很好用,因为您不必更改主题或动态地将视图添加到布局中。它应该是向前兼容的,并且一旦修复了这个错误就可以很容易地删除。

【讨论】:

  • 如果/当 Google 修复错误时,使用此修复的应用会在更新之前出现双 windowContentOverlays 吗?
  • @DaveFeldman 这取决于。如果他们增加 API 版本,那么不会,应用程序不会有双重覆盖。如果他们发布了不会更改 SDK 版本的增量更新,那么您可能会有用户重复覆盖。
  • 谢谢,这对我有用!我建议在你的基础活动中声明方法 setWindowContentOverlayCompat 为受保护,然后扩展此基础活动并在 onCreate 中的 setContentView 之后调用该方法。
【解决方案2】:

这是一个正式的错误,将在下一个平台版本中修复: https://code.google.com/p/android/issues/detail?id=58280

更新: 这似乎已在 API 级别 19 上修复

【讨论】:

  • 我得到了构建目标 API 19,但仍然没有找到资源:/
  • 我认为设备必须具有 API 19(4.4)。此问题发生在 4.3 上,但不在 4.2 或 4.1.2 上!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-18
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多