【问题标题】:Android Dialog with rounded corners - still showing the background without corners radius带有圆角的Android对话框 - 仍然显示没有角半径的背景
【发布时间】:2018-08-16 09:25:26
【问题描述】:

我想制作圆角对话框;但是我做完之后是这样的>>

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>

问题是:为什么对话框仍然显示在没有圆角半径的背景中?

在搜索了这个问题的解决方案后,我找到了其中的一些解决方案>>

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-经过上述解决方案的测试

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

解决方案测试后的结果

现在对话框根本没有出现! 谁能给我解决这个问题?提前谢谢你。

【问题讨论】:

    标签: java android android-layout android-dialog


    【解决方案1】:

    具有相同布局的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();
    

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

    【讨论】:

      【解决方案2】:

      在您尝试过的代码中,

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

      使用setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 方法,您基本上可以使该视图透明。

      您必须使用可绘制对象的形状和颜色创建一个 xml 文件,例如来自this post

      然后将其添加到布局中的根元素中,因此如果您刚刚创建的 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>
      

      然后正常创建对话框,没有从java设置样式,就像原来的那样:

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

      希望对你有帮助!

      【讨论】:

        【解决方案3】:

        在您的 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>
        

        【讨论】:

        • 您可以添加setBackgroundDrawablesetBackgroundDrawableResource。两者都做同样的事情。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-24
        • 2020-05-11
        • 2014-03-13
        • 2017-12-28
        • 2018-03-15
        • 2016-07-18
        • 1970-01-01
        相关资源
        最近更新 更多