安卓警报对话框后台问题 API 11+

我用下面的代码创建了一个。出于某种原因,我在Honeycomb及以上位置获得了额外的背景(见图)。对于蜂窝以下的任何内容,代码崩溃正常。 仅用于< API-11 和 API-11 及更高版本。AlertDialogMyCustomDialogTheme.DialogTheme.Holo.Dialog

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

更新找出了问题#2的答案。似乎构造函数是在 API 11 中引入的。我的修复只是简单地将行更改为:AlertDialog.Builder(Context context, int theme)

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

我仍然需要有关问题 #1 的帮助

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

找出答案

  1. AlertDialog 具有 AlertDialog 类中每个主题的静态常量,并且它不采用标准主题。当我用AlertDialog.THEME_HOLO_LIGHT替换或时,代码工作得很好。R.style.MyThemeandroid.R.style.Theme_Holo_Dialog
  2. 似乎构造函数是在 API 11 中引入的。我的修复只是简单地将行更改为:AlertDialog.Builder(Context context, int theme)

    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);
    }
    

答案 2

您可以尝试使用而不是new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.JumpDialog))new AlertDialog.Builder(this, R.style.JumpDialog)


推荐