Java 带有圆角的Android对话框-仍然显示没有圆角半径的背景

Java 带有圆角的Android对话框-仍然显示没有圆角半径的背景,java,android,android-layout,android-dialog,Java,Android,Android Layout,Android Dialog,我想做圆角对话框;但在我完成之后,它看起来是这样的>> Java AlertDialog.Builder dialogBuilder= new AlertDialog.Builder(this); dialogBuilder.setView(R.layout.complain_dialog); final AlertDialog alertDialog= dialogBuilder.create(); alertDialog.show(); XML <?xml version="1.0

我想做圆角对话框;但在我完成之后,它看起来是这样的>>

Java

AlertDialog.Builder dialogBuilder= new AlertDialog.Builder(this);
dialogBuilder.setView(R.layout.complain_dialog);
final AlertDialog alertDialog= dialogBuilder.create();
alertDialog.show();
XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100dp"
app:cardBackgroundColor="#FFF"
app:cardCornerRadius="15dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="15dp"
        android:background="@color/black_overlay" />

</android.support.v7.widget.CardView>
测试解决方案后的结果

Dialog dialog= new Dialog(getContext());
dialog.setContentView(R.layout.complain_dialog);
dialog.getWindow().setBackgroundDrawable(new 
ColorDrawable(Color.TRANSPARENT)); 
dialog.show();

现在该对话框根本不显示!
有谁能给我这个问题的解决方案吗?
提前谢谢。

使用相同布局的
AlertDialog
解决方案(在
Kitkat
上测试)


要显示相同的
对话框
,您需要设置对话框的宽度。否则将采用内容宽度。在将内容视图设置为对话框之前,请执行所有
dialog.getWindow()
操作

在java代码文件中添加以下拖行

customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
customDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
另外,请更新您的布局,如贝娄

 <?xml version="1.0" encoding="utf-8"?>
        <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            app:cardBackgroundColor="#FFF"
            app:cardCornerRadius="15dp"
            app:cardPreventCornerOverlap="false"
            app:cardUseCompatPadding="true">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginBottom="15dp"
                android:background="@color/black_overlay" />

        </android.support.v7.widget.CardView>

在您尝试的代码中

Dialog dialog= new Dialog(getContext());
dialog.setContentView(R.layout.complain_dialog);
dialog.getWindow().setBackgroundDrawable(new 
ColorDrawable(Color.TRANSPARENT)); 
dialog.show();
使用方法
setBackgroundDrawable(新的可着色(Color.TRANSPARENT))基本上是使视图透明

您必须创建一个xml文件,其中包含可绘制图形的形状和颜色,类似于

然后将其添加到布局中的根元素中,因此,如果刚刚创建的xml可绘制(可绘制文件夹)文件名为
background.xml
,则代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/background"
>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="15dp"
        android:background="@color/black_overlay" />

</android.support.v7.widget.CardView>

希望有帮助

您可以添加
setBackgroundDrawable
setBackgroundDrawable资源
。两者的作用相同。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/background"
>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="15dp"
        android:background="@color/black_overlay" />

</android.support.v7.widget.CardView>
AlertDialog.Builder dialogBuilder= new AlertDialog.Builder(this);
dialogBuilder.setView(R.layout.complain_dialog);
final AlertDialog alertDialog= dialogBuilder.create();
alertDialog.show()