如何从对话框中去除阴影?Android

3

我正在使用AppCompatDialog来构建对话框界面。

我的目标是在对话框显示时去掉周围的阴影?

以下是代码示例:

private void showWrongLoginPassDialog(String message){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getString(R.string.ad_login_error_title));
        builder.setMessage(message);
        builder.setPositiveButton(getString(R.string.ad_login_error_positive),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        builder.show();
    }

AlertDialog dialog = builder.create(); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); dialog.show(); - Blackbelt
你接近了)dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND‌​); dialog.show();这很有帮助) 谢谢! - Stan Malcolm
3个回答

13

试一下这个,它对我有效

dialog.getWindow().clearFlags(LayoutParams.FLAG_DIM_BEHIND);

为您提供以下代码:

builder.show().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

AlertDialog dialog = builder.create(); dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND‌​); dialog.show();这个没问题 - Stan Malcolm

1
这个问题的正确答案是:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

对我来说非常好用。希望能帮到其他人。

2
在API 22上,手机会生成一些深灰色的顶部填充,您的答案去掉了它,谢谢! - Starwave

0

一个可能的解决方案是使用自定义主题来创建对话框。这个自定义主题对我很有效。

 <style name="MenuDialog" parent="Theme.AppCompat.Light.Dialog">
    <!--This attribute removes the shadow-->
    <item name="android:background">@android:color/transparent</item>
    <!--This attribute updates the background color of the window-->
    <item name="android:windowBackground">@android:color/white</item>
    <!--Optional: This attribute makes the dialog window non-floating-->
    <item name="android:windowIsFloating">false</item>
</style>

现在,按照以下方式使用主题

 AlertDialog alertDialog = new AlertDialog.Builder(this, R.style.MenuDialog);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接