我想要一个带有圆角的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();
}
}
对话框布局(播放对话框)具有以下可绘制背景作为背景:
<?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"
我不希望看到该矩形。我该怎么做??
在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;
}
结果是完全一样的。如何删除该矩形?
谢谢!
最佳答案
当我遇到相同的问题时,我感到非常震惊,解决方案也有些奇怪。创建自己的自定义可绘制对象,例如参见下文。
<?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);