【问题标题】:Android Alert Dialog Background Issue API 11+Android 警报对话框背景问题 API 11+
【发布时间】:2012-01-30 21:41:38
【问题描述】:

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

  1. 知道为什么我会得到额外的背景吗?
  2. 知道为什么 API

更新找到了问题 #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;
}

【问题讨论】:

  • 能否请您发布 R.style.JumpDialog 代码

标签: java android android-theme android-dialog


【解决方案1】:

想出答案

  1. AlertDialog 中的每个主题都有静态常量 AlertDialog 类,它不采用标准主题。当我 替换 R.style.MyThemeandroid.R.style.Theme_Holo_Dialog 使用AlertDialog.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);
    }
    

【讨论】:

  • 从 1.6 开始,您应该改用 Build.VERSION.SDK_INT,因为 Build.VERSION.SDK 已被弃用。
  • 更新了答案以删除已弃用的 api 和硬编码的 api 级别
  • 所以“Integer.parseInt”不再需要了
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    对于那些寻找一种方法来自定义对话框主题而不必坚持使用默认主题(如在接受的解决方案中)的人来说,从 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/)来完成。

    【讨论】:

      猜你喜欢
      • 2017-05-10
      • 1970-01-01
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 2015-06-07
      • 1970-01-01
      • 2017-11-10
      相关资源
      最近更新 更多