簡體   English   中英

自定義警報對話框背景顏色

[英]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);

看到你可以像這樣為progressBar創建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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM