Android Alert对话框背景问题API 11+

问题描述:

我使用下面的代码创建了AlertDialog。 由于某种原因,我在Honeycomb及更高版本上获得了额外的背景(请参阅图片)。 代码崩溃适用于蜂窝下方的任何物品。 MyCustomDialog对于<只是Theme.Dialog API-11和Theme.Holo.Dialog API-11及更高版本。Android Alert对话框背景问题API 11+

  1. 任何想法,为什么我得到额外的背景?
  2. 任何想法为什么它崩溃的API < 11?如果我删除主题,它工作正常。

更新找到了问题#2的答案。似乎构造AlertDialog.Builder(Context context, int theme)在API 11介绍我的修正是简单地改变行:我还需要与问题#帮助

final AlertDialog.Builder builder = (Integer.parseInt(android.os.Build.VERSION.SDK) < 11)? new AlertDialog.Builder(this) : new AlertDialog.Builder(this,R.style.JumpDialog); 

enter image description here

private Dialog setupKeyBoardDialog() { 
    if (mContact.getLocaleId() != -1) { 
     final AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.MyCustomDialog); 
     builder.setTitle("Keyboards"); 

     mKeyboardLayouts = new KeyboardLayoutGroup(); 
     mKeyboardLayouts.layoutNames = new CharSequence[(int) jni.getNumKeyLayouts()]; 
     mKeyboardLayouts.layoutValue = new ArrayList<Integer>(); 

     for (int i = 0; i < jni.getNumKeyLayouts(); i++) { 
      mKeyboardLayouts.layoutNames[i] = jni.LayoutInfoForIndex(i).getName(); 
      mKeyboardLayouts.layoutValue.add(i, (int) jni.LayoutInfoForIndex(i).getLocale_id()); 
     } 

     final int selectedItem = mKeyboardLayouts.layoutValue.indexOf(mContact.getLocaleId()); 

     builder.setSingleChoiceItems(mKeyboardLayouts.layoutNames, selectedItem, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int item) { 
       mContact.setLocaleId(mKeyboardLayouts.layoutValue.get(item)); 
       mContactsDB.saveContact(mContact, true); 

       dialog.dismiss(); 
       initializeSettingsList(); 
      } 
     }); 

     final AlertDialog dialog = builder.create(); 
     dialog.setButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogBox, int arg1) { 
       dialogBox.cancel(); 
      } 
     }); 

     return dialog; 
    } 

    return null; 
} 

想通了回答

  1. AlertDialog对每个主题都有静态常量在 AlertDialog类中,它不采用标准主题。当我 取代R.style.MyThemeandroid.R.style.Theme_Holo_DialogAlertDialog.THEME_HOLO_LIGHT代码工作只是 罚款。
  2. 似乎构造AlertDialog.Builder(Context context, int theme)在API 11中引入我的修正只是在 行更改为:

    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

从1.6开始,您应该使用Build.VERSION.SDK_INT而不是 ,因为不推荐使用Build.VERSION.SDK。 – bCliks 2013-09-11 12:24:26

+0

更新了回答,以删除已弃用的api和硬编码api级别 – Ali 2013-09-12 02:15:45

+0

因此“Integer.parseInt”不再需要 – 2013-11-28 22:04:23

你可以尝试使用new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.JumpDialog))代替new AlertDialog.Builder(this, R.style.JumpDialog)

对于那些寻找一种自定义对话框主题的方式,而不必坚持使用默认设置(如在可接受的解决方案中),从棒棒糖开始,似乎多余的背景已被最终删除。所以,现在你可以创建一个主题,从默认的(例如使用程序兼容性)继承:

<!-- 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> 

而与此代码实例化建设者:

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

当然这一点也可以用的资源做文件夹(values/values-v21/)。