ActionBarSherlock doesn't support light-theme

2020-03-04 03:29发布

Well as the title says, i'm using the actionBarSherlock library and a light theme, and sometimes I need to show a dialog using the alertDialog.Builder class.

Thing is, no matter what I try, the theme doesn't apply to the dialog itself. The theme should work on both new APIs and old ones (prior to honeycomb).

example:

code:

final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity,
            R.style.AppTheme_LightDialog));

or:

final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity,
            R.style.Theme_Sherlock_Light_Dialog));

xml:

<style name="AppTheme_LightDialog" parent="@android:style/Theme.Light">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowTitleStyle">@style/DialogWindowTitle.Sherlock.Light</item>
    <item name="android:windowBackground">@drawable/abs__dialog_full_holo_light</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowActionModeOverlay">true</item>
    <item name="android:windowCloseOnTouchOutside">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:backgroundDimAmount">0.6</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:textColorPrimary">@color/abs__primary_text_holo_light</item>
    <item name="android:textColorPrimaryInverse">@color/abs__primary_text_holo_dark</item>
    <item name="windowMinWidthMajor">@dimen/abs__dialog_min_width_major</item>
    <item name="windowMinWidthMinor">@dimen/abs__dialog_min_width_minor</item>
    <item name="windowActionBar">false</item>
    <item name="windowContentOverlay">@null</item>
    <item name="android:textAppearance">?android:attr/textAppearanceInverse</item>
</style>

I know i can use the dialogFragment, but is there another way? the dialogs are quite simple, and as there are many of them, it would be annoying to change them all.


EDIT: I might be wrong, but it seems that using the android:alertDialogStyle attribute (shown here) changes it for old APIs , but it doesn't have a lot of customization. In fact, it supports only colors, and I'm not sure how to set even the text color (of the title and/or the message).

2条回答
Ridiculous、
2楼-- · 2020-03-04 04:11

After researching a bit, I think it's not an ActionBarScherlock issue, but a Light Theme issue in alert dialogs. Let's try some things:

Use:

final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.AppTheme_LightDialog));

Change:

<style name="AppTheme_LightDialog" parent="@android:style/Theme.Light">

To:

<style name="AppTheme_LightDialog" parent="@android:style/Theme.Dialog">

Then override the default "Theme.Dialog" styles (copy-pasted from the Android git tree):

<style name="AppTheme_LightDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item>
    <item name="android:windowBackground">@android:drawable/panel_background</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>

You may need to copy the original resources (@android:style/DialogWindowTitle, @android:style/Animation.Dialog and @android:drawable/panel_background) to your project.

And finally, the tricky part (from Shawn Castrianni ), as it seems Android needs some extra help to apply correctly a style to AlertDialog's text. Add to your "AppTheme_LightDialog" style:

<item name="android:textColor">?android:attr/textColorPrimaryInverseDisableOnly</item>

UPDATE:

It seems that prior to Honeycomb text styling is not actually applied to AlertDialogs. The above code gives you a solution to >=Honeycomb devices. There's an interesting work-around to make it work also in those devices (check this and this), but you may want to start asking you if you prefer a different approach which requires less work.

BTW, I'm not sure if it's your case, but it's important that you also use the same ContextThemeWrapper if you inflate a custom layout for the AlertDialog. For example,

Change:

View view = View.inflate(activity, R.layout.myDialog, null);

To:

View view = View.inflate(new ContextThemeWrapper(activity, R.style.AppTheme_LightDialog), R.layout.myDialog, null);
查看更多
ゆ 、 Hurt°
3楼-- · 2020-03-04 04:27

This is what I did and it made the body of the dialog white. The title is still on a black background:

new AlertDialog.Builder(
    new ContextThemeWrapper(
        activity,
        R.style.Theme_Sherlock_Light));

I also tried Theme_Sherlock_Light_NoActionBar, but it doesn't seem to make any difference.

查看更多
登录 后发表回答