6

我想要一个Dialog带圆角的,但是当Dialog看到它时,它下面有一个矩形,它在角落下面可以看到,如下所示:

在此处输入图像描述

dialog使用自定义构建DialogFragment

public class MyDialogFragment extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.playdialog, null));
    return builder.create();
}
}

对话框布局(playdialog)具有以下可绘制作为背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >

    <solid
        android:color="#AAAAAAAA" />

    <stroke
        android:width="2dp"
        android:color="#FF000000" />

    <corners android:radius="20dp" />
</shape>

正如我所说,我将此可绘制对象设置为背景:

android:background="@drawable/dialog_background"

我不想看到那个矩形。我该怎么做??

这篇文章中,用户遇到了同样的问题。我尝试使用对他有用的解决方案,但对我不起作用。我DialogFragment像这样修改了我的:

public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.playdialog, null));
    Dialog d = builder.create();
    d.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    return d;
}

结果是完全一样的。如何删除该矩形?

谢谢!

4

5 回答 5

7

当我遇到同样的问题时我很震惊,解决方案也有点奇怪。创建您自己的自定义可绘制对象,例如见下文。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="@color/text_highlight" />

    <stroke
        android:width="5dp"
        android:color="@color/text_highlight" />

    <corners android:radius="12dp" />

    <stroke
        android:width="1dp"
        android:color="@android:color/transparent" />

</shape>

将以下行添加到您的对话框中:

 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

 // This is line that does all the magic
 dialog.getWindow().setBackgroundDrawableResource(                             
 R.drawable.dialog_rounded);
于 2014-12-23T05:59:01.663 回答
4

尝试将您的样式设置DialogFragmentTheme_Translucent.

setStyle(STYLE_NO_FRAME, android.R.style.Theme_Translucent);

这个对我有用。

于 2013-06-25T09:56:06.687 回答
1

添加
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

onCreateView()你的 MyDialogFragment 的方法

于 2016-04-04T15:58:43.757 回答
1

我在代码中为对话框设置了透明背景。尝试这个:

@Override
public void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00ffffff));
}
于 2015-10-28T06:55:18.560 回答
0

尝试这个:

customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
于 2014-12-01T05:32:55.340 回答