Skip to content

Instantly share code, notes, and snippets.

@yyaammaa
Last active December 27, 2015 10:49
Show Gist options
  • Save yyaammaa/7314571 to your computer and use it in GitHub Desktop.
Save yyaammaa/7314571 to your computer and use it in GitHub Desktop.
Android 4.3 (API level 18) でAction Barの影が出ない

Android 4.3 (API level 18) でAction Barの影が出ない

問題

Issue 58280: ActionBar does not display windowContentOverlay drawable
android:windowContentOverlay がAndroid 4.3 (API level 18) で無視されるため、Action Barの影をカスタムで指定できない

styles.xml

<resources>
    <style name="AppTheme" parent="android:Theme.Light">
        <!-- 4.3でこいつが無視される -->
        <item name="android:windowContentOverlay">@drawable/ab_solid_shadow_holo</item>
    </style>
</resources>

正常な場合 (Android 4.3以外)

影が出ない (Android 4.3)

解決策

Stackoverflow: What happened to windowContentOverlay in Android API 18?
以下のようなworkaroundを入れる。setContentView()のあとであればよいので、各Activityに書いてもよいがさすがに面倒なのでBaseActivityでonPostCreateをOverrideしたりするのが楽。

public abstract class AbstractBaseActivity extends Activity {

  @Override
  protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    
    // setContentView()のあとに実行する
    setWindowContentOverlayCompat();
  }

  /**
   * 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) {
      View contentView = findViewById(android.R.id.content);
      if (contentView instanceof FrameLayout) {
        TypedValue tv = new TypedValue();
        if (getTheme().resolveAttribute(android.R.attr.windowContentOverlay, tv, true)) {
          if (tv.resourceId != 0) {
            ((FrameLayout) contentView).setForeground(getResources().getDrawable(tv.resourceId));
          }
        }
      }
    }
  }

}

補足

  • Android 4.4 (API level 19) では治った。良かった。が、4.3が存在する限りこのworkaroundは消せない :(
  • android:windowContentOverlay でカスタムの影を指定していないのであればAndroid 4.3でもちゃんと影は出る。が、以下のようにappcompatを使っているとこの問題に向き合う必要がある

別の問題 (appcompat + Gingerbreadなどで影が出ない)

  • appcompatを使うとGingerbreadとかでそもそもAction Barの影が出なかったりするが、その場合はandroid:windowContentOverlayで指定してあげると出るようになる
    • デフォルトの影を出したいのであれば、ab_solid_shadow_holo.9.png などをSDKからコピペしたりする
  • ということでappcompatで影が出ない問題を解決するためにandroid:windowContentOverlayで指定すると、Android 4.3で影が出ない問題に直面する
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment