带有圆角的Android AlertDialog:在圆角下方看到矩形 [英] Android AlertDialog with rounded corners: rectangle seen below corners
问题描述
我想要一个带有圆角的Dialog
,但是当看到Dialog
时,在它的下方有一个矩形,它在角的下方可见,就像这样:
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:
我使用自定义的DialogFragment
构建dialog
:
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??
在此帖子中,用户遇到了同样的问题.我尝试使用对他有用的解决方案,但对我不起作用.我这样修改了DialogFragment
:
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?
谢谢!
推荐答案
当我遇到相同的问题时,我感到非常震惊,解决方案也有些奇怪.创建自己的自定义可绘制对象,例如参见下文.
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);
这篇关于带有圆角的Android AlertDialog:在圆角下方看到矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!