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

标签 java android android-layout android-dialog

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

Result Design

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

测试解决方案后的结果

Result Design

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

最佳答案

使用具有相同布局的 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 。在将内容 View 设置为 Dialog 之前执行所有 dialog.getWindow() 操作。

关于java - 带圆角的 Android 对话框 - 仍然显示没有圆角半径的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51873730/

相关文章:

java - 如何在android中使CalendarView垂直滚动

java - 与mysql中的整数值进行比较

java - 监控流程或方法所花费的时间

android - 在 android-billing-5.0 中获取 ProductDetails 价格

android - 创建项目 Android 时出错

android - relativeLayout 设置为 wrap_content 与 alignParentRight?

android - 使用TextView的onClick跳转到下一页

java - 如何使用 K-Means 算法查找异常/离群值

java - 如何确保我正在检查未选中的复选框 selenium java

java - R 中的 Weka 不工作 - 无法通过 WPM 加载 weka 包