我用下面的代码创建了一个AlertDialog。由于某些原因,我得到了一个额外的背景(见图)蜂窝和以上。对于蜂窝下面的任何内容,代码都会使崩溃。MyCustomDialog只是< API-11的Theme.Dialog和API-11及更高版本的Theme.Holo.Dialog。
Update找到了问题#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;
}发布于 2011-12-31 08:26:40
找出答案
R.style.MyTheme或android.R.style.Theme_Holo_Dialog替换AlertDialog.THEME_HOLO_LIGHT时,代码工作得很好。AlertDialog.Builder(Context context, int theme)。最后的AlertDialog.Builder构建器;if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { builder =新的AlertDialog.Builder(this);}AlertDialog.Builder{ builder = new AlertDialog.Builder(this,R.style.JumpDialog);}
发布于 2013-08-21 16:32:35
您可以尝试使用new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.JumpDialog))而不是new AlertDialog.Builder(this, R.style.JumpDialog)
发布于 2015-02-17 12:41:40
对于那些寻找自定义对话框主题的方法的人来说,不必坚持默认的对话框主题(就像在接受的解决方案中那样),从Lollipop开始,额外的背景似乎最终被删除了。因此,现在您可以创建一个从默认主题继承的主题(例如,使用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>并使用以下代码实例化构建器:
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/)来完成。
https://stackoverflow.com/questions/8685351
复制相似问题