繁体   English   中英

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

[英]Android Alert Dialog Background Issue API 11+

我使用下面的代码创建一个AlertDialog 出于某种原因,我在Honeycomb及以上版本上获得了额外的背景(见图)。 代码在蜂窝以下的任何地方崩溃都很好。 MyCustomDialog只是Theme.Dialog为<API-11和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的帮助

在此输入图像描述

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); } 

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM