带有圆角的Android AlertDialog:角落下方的矩形

时间:2013-02-28 19:29:40

标签: android alertdialog custom-view

我想要一个带圆角的Dialog,但是当看到Dialog时,在角落下方会看到一个矩形,如下所示:

enter image description here

我使用自定义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>

正如我所说,我将此drawable设置为背景:

android:background="@drawable/dialog_background"

我不希望看到那个矩形。我该怎么办?

this帖子中,用户遇到了同样的问题。我试图使用适合他的解决方案,但它对我不起作用。我像这样修改了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;
}

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

谢谢!

5 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:2)

尝试将DialogFragment的样式设置为Theme_Translucent.

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

它对我有用。

答案 2 :(得分:0)

试试这个:

customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

答案 3 :(得分:0)

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

@Override
public void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00ffffff));
}

答案 4 :(得分:0)

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

到MyDialogFragment的onCreateView()方法

相关问题