java - Android 警报对话框背景问题 API 11+

标签 java android android-theme android-dialog

我用下面的代码创建了一个AlertDialog。 出于某种原因,我在 Honeycomb 及更高版本上获得了额外的背景(参见图片)。 代码崩溃适用于低于 hive 的任何东西。 MyCustomDialog 只是 Theme.Dialog 用于 Theme.Holo.Dialog 用于 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);

我仍然需要有关问题 #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. 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);
    }
    

关于java - Android 警报对话框背景问题 API 11+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8685351/

相关文章:

java - 使用 EclipseLink JAXB (MOXy) 解码 XML 时出现 DescriptorException "Missing class indicator field from database row"异常

java - Spring MVC 中带有 POST 和 JSON 的 RequestMapping 未运行

java - Mockito:通缉但未调用

android - TouchDB - CouchbaseLite 库?

Android - 工具栏的标准高度

java - 如何从 Spring Boot 调用用户定义的 sql 函数?

java - 添加新 Activity 期间 Eclipse 错误

android - 更改按钮的启用颜色而不更改禁用颜色

android - Lollipop 进度条着色

android - 在 Android 中使用新 Material 设计主题的颜色