问题
I want a Dialog
with rounded corners, but when the Dialog
is seen there's a rectangle below it that's seen below the corners, like this:

I build the dialog
using a custom 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();
}
}
The dialog layout (playdialog) has the following drawable as background:
<?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>
As I said, I set this drawable as background:
android:background="@drawable/dialog_background"
I don't want that rectangle to be seen. How can I do it??
In this post the user had the same problem. I tried to use the solution that worked for him but it didn't work for me. I modified my DialogFragment
like this:
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;
}
The result is exactly the same. How can I remove that rectangle?
Thanks!
回答1:
I was shocked when I occured the same problem, the solution is also a little weird. Create your own custom drawable for eg see below.
<?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>
Add the following lines to your dialog:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// This is line that does all the magic
dialog.getWindow().setBackgroundDrawableResource(
R.drawable.dialog_rounded);
回答2:
Try to set the style of your DialogFragment
to Theme_Translucent.
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Translucent);
It works for me.
回答3:
Try this:
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
回答4:
i set a transparent background to the dialog in code. try this:
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00ffffff));
}
回答5:
AddgetDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
to the onCreateView()
method of your MyDialogFragment
来源:https://stackoverflow.com/questions/15144033/android-alertdialog-with-rounded-corners-rectangle-seen-below-corners