自定义警报对话框背景颜色

custom alert dialog background color

我需要一个自定义警报对话框来显示进度。当我 运行 时,警告对话框的背景颜色不会变为透明... 下面我将我的代码放在 xml 布局和警报对话框代码中

xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@android:color/transparent"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">

<androidx.cardview.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:cardElevation="10dp"
    app:cardCornerRadius="100dp">

        <ProgressBar
            android:id="@+id/progressBar"
            style="@style/Widget.AppCompat.ProgressBar"
            android:layout_width="30dp"
            android:layout_margin="5dp"
            android:layout_height="30dp" />

</androidx.cardview.widget.CardView>

这是我的片段代码:

                View progress = getLayoutInflater().inflate(R.layout.progress_alert, null);
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setCancelable(false);
            builder.setView(progress);
            alertDialog = builder.create();
            alertDialog.show();

您可以将警告对话框的背景颜色更改为透明:

alertDialog.getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);

您可以像这样为进度条创建 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativeLayoutProgressBar"
android:visibility="gone"
android:elevation="8dp"
>

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    style="?android:attr/progressBarStyle"
    android:indeterminate="true"
    android:layout_centerInParent="true"
    android:theme="@style/RedAccent"
    android:elevation="5dp"
    />

<View
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#CCFFFFFF"
    android:elevation="4dp"
    />

</RelativeLayout>

并在 xml 文件中包含此布局行,您需要这样的进度条:

<include layout="progressbar" />

并像这样访问:

RelativeLayout layout = findViewById(R.id.relativeLayoutProgressBar);
layout.setVisibility(View.GONE);

试试这个,

final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.getWindow().setBackgroundDrawable(new 
                                              ColorDrawable(Color.TRANSPARENT));
alertDialog.show();

尝试在 styles.xml

中设置这样的对话框主题
<style name="dialogTheme" parent="android:Theme.Holo.Dialog">
    <item name="android:windowMinWidthMajor">90%</item>
    <item name="android:windowMinWidthMinor">90%</item>
</style>

java代码

Dialog dialog = new Dialog(activity, R.style.dialogTheme);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.your_dialog_layout);
activity.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));