简体   繁体   中英

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

2- Android custom alert dialog with rounded corners

3- Android dialog background with corner radius has layered background

Java- After test the above solutions

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.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. Here is a reference . Otherwise it will take the Content width . Do all dialog.getWindow() operation before setting Content view to Dialog .

Add bellow tow line in your java code file

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)); 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 .

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

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

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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