首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓警报对话框背景问题API 11+

安卓警报对话框背景问题API 11+
EN

Stack Overflow用户
提问于 2011-12-31 00:24:33
回答 3查看 6.5K关注 0票数 13

我用下面的代码创建了一个AlertDialog。由于某些原因,我得到了一个额外的背景(见图)蜂窝和以上。对于蜂窝下面的任何内容,代码都会使崩溃。MyCustomDialog只是< API-11的Theme.Dialog和API-11及更高版本的Theme.Holo.Dialog

  1. 知道我为什么要得到额外的背景吗?
  2. 知道为什么它会崩溃吗?如果我移除主题,它就能正常工作。

Update找到了问题#2的答案。似乎构造函数AlertDialog.Builder(Context context, int theme)是在API 11中引入的。

代码语言:javascript
复制
final AlertDialog.Builder builder = (Integer.parseInt(android.os.Build.VERSION.SDK) < 11)? new AlertDialog.Builder(this) : new AlertDialog.Builder(this,R.style.JumpDialog);

我仍然需要帮助解决问题1

代码语言:javascript
复制
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;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-12-31 08:26:40

找出答案

  1. AlertDialog为AlertDialog类中的每个主题设置了静态常量,并且不接受标准主题。当我用R.style.MyThemeandroid.R.style.Theme_Holo_Dialog替换AlertDialog.THEME_HOLO_LIGHT时,代码工作得很好。
  2. 似乎是在API 11中引入了构造函数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);}

票数 17
EN

Stack Overflow用户

发布于 2013-08-21 16:32:35

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

票数 6
EN

Stack Overflow用户

发布于 2015-02-17 12:41:40

对于那些寻找自定义对话框主题的方法的人来说,不必坚持默认的对话框主题(就像在接受的解决方案中那样),从Lollipop开始,额外的背景似乎最终被删除了。因此,现在您可以创建一个从默认主题继承的主题(例如,使用AppCompat):

代码语言:javascript
复制
<!-- 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>

并使用以下代码实例化构建器:

代码语言:javascript
复制
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/)来完成。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8685351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档