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>
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は消せない :(
- "ActionBar's window content overlay is allowed to be drawn" とか言っているが単にこのバグを治しただけ
- android:windowContentOverlay でカスタムの影を指定していないのであればAndroid 4.3でもちゃんと影は出る。が、以下のようにappcompatを使っているとこの問題に向き合う必要がある
- appcompatを使うとGingerbreadとかでそもそもAction Barの影が出なかったりするが、その場合はandroid:windowContentOverlayで指定してあげると出るようになる
- デフォルトの影を出したいのであれば、ab_solid_shadow_holo.9.png などをSDKからコピペしたりする
- ということでappcompatで影が出ない問題を解決するためにandroid:windowContentOverlayで指定すると、Android 4.3で影が出ない問題に直面する