Android Alert Dialog Background Issue API 11+

后端 未结 3 1331
时光取名叫无心
时光取名叫无心 2020-12-29 09:04

I create a AlertDialog with the code below. For some reason I\'m getting an extra background (see pic) on Honeycomb and above. The code crashes

相关标签:
3条回答
  • 2020-12-29 09:39

    Figured out the answers

    1. AlertDialog has it's on static constants for each theme in the AlertDialog class and it does not take the standard theme. when I replaced R.style.MyTheme or android.R.style.Theme_Holo_Dialog with AlertDialog.THEME_HOLO_LIGHT the code worked just fine.
    2. Seems the constructor AlertDialog.Builder(Context context, int theme) was introduced in API 11. My fix was simply to change the line to:

      final AlertDialog.Builder builder;
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
          builder = new AlertDialog.Builder(this);
      } else {
          builder = new AlertDialog.Builder(this,R.style.JumpDialog);
      }
      
    0 讨论(0)
  • 2020-12-29 09:44

    For those looking for a way to customize the dialog theme without having to stick with the default ones (as in the accepted solution), starting with Lollipop it seems that the extra background has been finally removed. So, now you can create a theme inheriting from the default one (example with AppCompat):

    <!-- default theme for L devices -->
    <style name="SelectionDialog" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:textColor">@color/default_text_color_holo_light</item>
    </style>
    <!-- theme for Pre-L devices -->
    <style name="SelectionDialog.PreL">
        <!-- remove the dialog window background -->
        <item name="android:windowBackground">@color/transparent</item>
    </style>
    

    And instantiate your builder with this code:

    AlertDialog.Builder builder = new AlertDialog.Builder(
                getActivity(),                
                Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT ?
                        R.style.SelectionDialog :
                        R.style.SelectionDialog_PreL);
    

    Of course this could also be done with resource folders (values/ and values-v21/).

    0 讨论(0)
  • 2020-12-29 09:57

    You can try to use new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.JumpDialog)) instead of new AlertDialog.Builder(this, R.style.JumpDialog)

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