Android Custom Alert Dialog Display Error after changing the Build Version

I am developing a simple demo . Here in this demo, I am just creating one simple custom alert dialog . It works fine.

It shows me the perfect result when i build application in 1.6, but when i change the android version from 1.6 to 2.2, it shows the unexpected result. It doesn't show the background screen on which i display the custom alert dialog.

Here is my xml file. Custom Dialog Theme File

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomDialogTheme" parent="@android:style/AlertDialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowAnimationStyle">@android:style/Theme.Dialog</item>
    </style>
</resources>

Here is My CustomConfirmOkDialog Class

package com.utility.org;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

    public class CustomConfirmOkDialog extends Dialog implements OnClickListener
    {
        private Button okButton = null;
        private TextView infoText=null,confirmBody=null;
        private int errorMessage=0;
        @SuppressWarnings("unused")
        private Activity activity;

        public CustomConfirmOkDialog(Activity context,int customdialogtheme,int errorMessage) 
        {
            super(context,customdialogtheme);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.confirm_ok);
            this.errorMessage = errorMessage;
            this.activity = context;
            initControls();
        }

        private void initControls()
        {
            okButton = (Button) findViewById(R.id.ok_button);
            okButton.setOnClickListener(this);

            infoText = (TextView)findViewById(R.id.infoText);
            confirmBody = (TextView)findViewById(R.id.confirmBody);

            switch (this.errorMessage) 
            {

                case Utility.INVALID_USERNAME_PASSWORD:
                    try
                    {
                        infoText.setText(R.string.signIn);
                        confirmBody.setText(R.string.invalidUsernameAndPassword);
                    }
                    catch (Exception e) 
                    {
                        e.printStackTrace();
                    }
                    break;


                default:
                    break;
            }
        }
        public void onClick(View v) 
        {
            dismiss();
        }
    }

Calling this class from my main activity using the below code.

CustomConfirmOkDialog dialog = new CustomConfirmOkDialog(MainActivity.this, R.style.CustomDialogTheme, Utility.INVALID_USERNAME_PASSWORD);
dialog.show();

在这里输入图像描述

Here you can clearly notice that 1st image shows the background . Its build in android 1.6 version while 2nd image doesn't shows the background . It shows the entire black screen. Its build in android version 2.2 . I am very thankful if anyone can solve this issue.

Can anyone help me to solve this simple and silly issue ?

Thanks in Advance.


它通过更改Custom Dialog Theme xml文件中的以下代码解决了我的问题。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomDialogTheme" parent="@android:style/Theme.Translucent.NoTitleBar">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>

I also faced the same problem. the problem is when I called constructor of Dialog class

Dialog(Context context, int themeId)

it will hide the background activity. The only solution that i found is don't call this constructor, instead only call

Dialog(Context context)

and set your style in the layout file.

So in your code, only write

super(context)

instead of

super(context, themeid);


Apparently, this is a known issue.

This only happens when you try inheriting from the framework themes. Using @android:style directly will still treat them as non- fullscreen, which punches through the black background as expected.

One workaround is to start with a nearly-blank theme (like Panel or Translucent) and then render what you need in your own layout (such as dialog edges, etc).

Thought, I still don't fully understand this solution myself yet.

And actually, I'm no longer sure they're talking about the exact same bug you've seen, since they're talking about it not working for an older version of the sdk (not a newer one like yours). See the bug report.

链接地址: http://www.djcxy.com/p/26924.html

上一篇: 蓝牙许可对话相乘

下一篇: 更改构建版本后,Android自定义警报对话框显示错误