我用下面的代码创建了一个AlertDialog
。
出于某种原因,我在 Honeycomb 及更高版本上获得了额外的背景(参见图片)。
代码崩溃适用于低于 hive 的任何东西。
MyCustomDialog
只是 Theme.Dialog
用于
- 知道为什么我会得到额外的背景吗?
- 知道为什么 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 的帮助
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;
}
最佳答案
找出答案
- AlertDialog 中的每个主题都有静态常量
AlertDialog 类,它不采用标准主题。当我
替换为
R.style.MyTheme
或android.R.style.Theme_Holo_Dialog
与AlertDialog.THEME_HOLO_LIGHT
代码只是工作 很好。 似乎是构造函数
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/