ActionBarSherlock doesn't support light-theme alert dialogs?

后端 未结 2 2106
春和景丽
春和景丽 2021-01-06 07:34

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

相关标签:
2条回答
  • 2021-01-06 07:58

    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);
    
    0 讨论(0)
  • 2021-01-06 08:05

    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.

    0 讨论(0)
提交回复
热议问题