简体   繁体   English

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

[英]Android Dialog with rounded corners - still showing the background without corners radius

I want to make rounded corners dialog;我想制作圆角对话框; but after I was done, it appears like this>>但在我完成后,它看起来像这样>>

结果设计

Java爪哇

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

XML 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>

The problem is: why the dialog is still shown int the background without corners radius?问题是:为什么对话框仍然显示在没有圆角半径的背景中?

After searching for a solution to this problem, I found some of these solutions>>在寻找解决此问题的方法后,我找到了其中一些解决方案>>

1- Android Dialog - Rounded Corners and Transparency 1- Android 对话框 - 圆角和透明度

2- Android custom alert dialog with rounded corners 2- 带有圆角的Android自定义警报对话框

3- Android dialog background with corner radius has layered background 3- 带有圆角半径的Android对话框背景具有分层背景

Java- After test the above solutions Java-经过上述解决方案的测试

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

Result after test the solutions测试解决方案后的结果

结果设计

Now the dialog is not appear at all!现在对话框根本没有出现! Can any one give me solution for this problem?任何人都可以给我解决这个问题吗? Thank you in advance.先感谢您。

Solution with AlertDialog with same layout (tested On Kitkat ).具有相同布局的AlertDialog解决方案(在Kitkat测试)。

 AlertDialog.Builder dialogBuilder= new AlertDialog.Builder(this);
 dialogBuilder.setView(R.layout.temp);
 final AlertDialog alertDialog= dialogBuilder.create();
 alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
 alertDialog.show();

To appear Dialog as same you need set the width of dialog.要显示相同的Dialog ,您需要设置对话框的宽度。 Here is a reference . 这里有一个参考 Otherwise it will take the Content width .否则它将采用 Content width 。 Do all dialog.getWindow() operation before setting Content view to Dialog .在将 Content 视图设置为Dialog之前执行所有dialog.getWindow()操作。

Add bellow tow line in your java code file在您的 Java 代码文件中添加波纹管

customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
customDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

Also please update your layout like bellow也请更新您的布局,如下所示

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

In the code that you tried,在您尝试的代码中,

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

With the method setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));使用方法setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); you are basically making that view transparent.您基本上是在使该视图透明。

You have to create a xml file with the shape and color of your drawable, something like from this post .您必须使用 drawable 的形状和颜色创建一个 xml 文件,类似于这篇文章

And then add it to your root element in your layout, so if your xml drawable (drawable folder) file that you just created is called background.xml your code would look like this:然后将其添加到布局中的根元素,因此如果您刚刚创建的 xml drawable(drawable 文件夹)文件名为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>

And then just create the dialog normally, with no styles set from java, like originally:然后正常创建对话框,没有从java设置样式,就像原来一样:

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

Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM