【问题标题】:Dialog with transparent background in AndroidAndroid中具有透明背景的对话框
【发布时间】:2012-06-03 10:28:26
【问题描述】:

如何从 Android 中的对话框中删除黑色背景。图片显示了问题。

final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger); 

【问题讨论】:

标签: android android-dialog


【解决方案1】:

添加此代码

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

或者这个:

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

【讨论】:

  • 谢谢!不过,我更喜欢使用dialog.getWindow().setBackgroundDrawable(new ColorDrawableResource(R.color.transparent));
  • 这个解决方案有帮助。问题是,宽度将适合屏幕。与普通对话框相比,不会有填充。但 Android 4.1 默认处理它
  • 如果我使用 ALert 对话框呢??
  • 如果您在 DialogFragment 中,只需调用 getDialog().getWindow() ...(在创建视图之后,例如在您的 onViewCreated 回调中)。
  • 我更喜欢使用dialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
【解决方案2】:
<style name="NewDialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:background">@android:color/transparent</item>
</style>

在java中使用

Dialog dialog = new Dialog(this, R.style.NewDialog);

希望能帮到你!

【讨论】:

  • 它会起作用,但是如果你点击那个透明区域对话框将不会关闭如何处理这个?
  • @John 你可以使用:dialog.setCanceledOnTouchOutside(true);
  • 您可以使用 parent="Theme.AppCompat.Dialog" 默认在触摸外部时退出。
  • 甚至可以处理暗背景的 alpha:&lt;item name="android:backgroundDimAmount"&gt;0.2&lt;/item&gt; 或以编程方式 window.getAttributes().dimAmount = 0.2f;
【解决方案3】:

我遇到了更简单的问题,我想出的解决方案是应用透明的背景主题。用你的风格写下这些行

    <item name="android:windowBackground">@drawable/blue_searchbuttonpopupbackground</item>
</style>
<style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

然后加上

android:theme="@style/Theme.Transparent"

在您的主清单文件中,在对话活动的块内。

在您的对话活动 XML 集中添加

 android:background= "#00000000"

【讨论】:

  • 如果你只想设置对话框的样式,也可以使用这个:
【解决方案4】:

不知何故,Zacharias 解决方案对我不起作用,所以我使用下面的主题来解决这个问题...

<style name="DialogCustomTheme" parent="android:Theme.Holo.Dialog.NoActionBar">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
</style>

可以将此主题设置为对话框,如下所示

final Dialog dialog = new Dialog(this, R.style.DialogCustomTheme); 

享受!!

【讨论】:

  • ​​@color/transparent 此行应替换为以下行,否则您必须在 colors.xml 文件中添加一个透明值。 ​​@android:color/transparent
【解决方案5】:

如果你想破坏对话框的深色背景,使用这个

dialog.getWindow().setDimAmount(0);

【讨论】:

  • 谢谢。这是我一直在寻找的。太棒了。
  • 这是最简单的方法,谢谢!但是,如果您希望您的应用在 100% 的设备上运行(我已将最低 SDK 版本设置为 15),Android 建议如下:if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.KITKAT) { Objects.requireNonNull(alertDialog.getWindow()).setDimAmount(0); }
【解决方案6】:

您可以使用:

setBackgroundDrawable(null);

方法。以下是文档:

  /**
    * Set the background to a given Drawable, or remove the background. If the
    * background has padding, this View's padding is set to the background's
    * padding. However, when a background is removed, this View's padding isn't
    * touched. If setting the padding is desired, please use
    * {@link #setPadding(int, int, int, int)}.
    *
    * @param d The Drawable to use as the background, or null to remove the
    *        background
    */

【讨论】:

  • 这会起作用,但只有当您扩展对话框时,这不是一个快速的解决方案,而是一个很好的解决方案......
【解决方案7】:

对话框弹出填充默认黑色背景颜色或主题颜色,因此您需要在对话框中设置TRANSPARENT背景。试试下面的代码:-

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.splash);
dialog.show();

【讨论】:

    【解决方案8】:

    我发现所有现有答案的一个问题是没有保留边距。这是因为它们都用纯色覆盖了负责边距的android:windowBackground 属性。但是,我在 Android SDK 中进行了一些挖掘,发现默认的窗口背景可绘制,并对其进行了一些修改以允许透明对话框。

    首先,将 /platforms/android-22/data/res/drawable/dialog_background_material.xml 复制到您的项目中。或者,只需将这些行复制到一个新文件中:

    <inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:inset="16dp">
        <shape android:shape="rectangle">
            <corners android:radius="2dp" />
            <solid android:color="?attr/colorBackground" />
        </shape>
    </inset>
    

    请注意,android:color 设置为 ?attr/colorBackground。这是您看到的默认纯灰色/白色。要让自定义样式中android:background 中定义的颜色透明并显示透明度,我们只需将?attr/colorBackground 更改为@android:color/transparent。现在它看起来像这样:

    <inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:inset="16dp">
        <shape android:shape="rectangle">
            <corners android:radius="2dp" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </inset>
    

    之后,转到您的主题并添加以下内容:

    <style name="MyTransparentDialog" parent="@android:style/Theme.Material.Dialog">
        <item name="android:windowBackground">@drawable/newly_created_background_name</item>
        <item name="android:background">@color/some_transparent_color</item>
    </style>
    

    确保将newly_created_background_name 替换为您刚刚创建的可绘制文件的实际名称,并将some_transparent_color 替换为所需的透明背景。

    之后,我们需要做的就是设置主题。在创建AlertDialog.Builder 时使用它:

        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyTransparentDialog);
    

    然后像往常一样构建、创建和显示对话框!

    【讨论】:

    • 我随后使用 AppCompat AlertDialog 进行此操作,但没有使用 android:background,而是选择直接使用背景可绘制对象的 android:color 本身设置透明颜色。需要这样做,因为设置 android:background 以某种方式使某些区域仍然不透明。但感谢分享技术!
    【解决方案9】:

    注意:不要使用builder来改变背景。

    Dialog dialog = new Dialog.Builder(MainActivity.this)
                                    .setView(view)
                                    .create();
    dialog.show();dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    

    改成

    Dialog dialog = new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(view);
    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    dialog.show();
    

    使用 Dialog.builder 时,它没有在其中提供 getWindow() 选项。

    【讨论】:

      【解决方案10】:

      在你的代码中试试这个:

      getWindow().setBackgroundDrawableResource(android.R.color.transparent);
      

      它肯定会工作......在我的情况下......!我的朋友

      【讨论】:

        【解决方案11】:

        与 zGnep 相同的解决方案,但使用 xml:

        android:background="@null"
        

        【讨论】:

          【解决方案12】:

          这就是我使用 AlertDialog 实现半透明所做的。

          创建了自定义样式:

          <style name="TranslucentDialog" parent="@android:style/Theme.DeviceDefault.Dialog.Alert">
              <item name="android:colorBackground">#32FFFFFF</item>
          </style>
          

          然后创建对话框:

          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.TranslucentDialog);
          AlertDialog dialog = builder.create();
          

          【讨论】:

            【解决方案13】:

            使用对我有用的代码:

                Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
                dialog.show();
            

            【讨论】:

            • 我认为,当您为您的意图添加一些解释时,这对 OP 和更多访问者会更有帮助。
            【解决方案14】:

            你可以使用(可选)

            dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
            

            我会推荐创建一个扩展函数。就像是 extensions.kt

            import android.app.Dialog
            
            fun Dialog.setTransparentBackground() {
                window?.setBackgroundDrawableResource(android.R.color.transparent)
            }
            

            在任何对话框中使用它

            dialog.setTransparentBackground()
            

            有一些有趣的编程......

            【讨论】:

              【解决方案15】:

              在我的情况下,解决方案是这样的:

              dialog_AssignTag.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
              

              此外 在自定义对话框的 XML 中:

              android:alpha="0.8"
              

              【讨论】:

                【解决方案16】:

                如果你扩展了 DialogFrament 类,你可以设置主题:

                setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialogTheme);
                

                然后在您的 styles.xml 文件中创建自定义主题(参见@LongLv 的参数答案)

                如果您希望在用户触摸对话框外部时关闭对话框,请不要忘记添加&lt;item name="android:windowCloseOnTouchOutside"&gt;true&lt;/item&gt;

                【讨论】:

                  【解决方案17】:

                  在style中设置这些样式代码

                  <style name="Theme.Transparent" parent="android:Theme">
                      <item name="android:windowIsTranslucent">true</item>
                      <item name="android:windowBackground">@android:color/transparent</item>
                      <item name="android:windowContentOverlay">@null</item>
                      <item name="android:windowNoTitle">true</item>
                      <item name="android:windowIsFloating">true</item>
                      <item name="android:backgroundDimEnabled">false</item>
                  </style>
                  

                  只需将下面的 false 更改为 true

                  <item name="android:backgroundDimEnabled">true</item>
                  

                  它会使你的背景变暗。

                  【讨论】:

                    【解决方案18】:
                    Window window = d.getWindow();
                    window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
                    

                    这是我的方法,你可以试试!

                    【讨论】:

                    • 现在已弃用:不再支持@deprecated Blurring。
                    【解决方案19】:

                    对于使用带有自定义类的自定义对话框的任何人,您需要更改类中的透明度,在 onCreate() 中添加以下行:

                    getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                    

                    【讨论】:

                      【解决方案20】:

                      dialog.getWindow().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(ctx, android.R.color.transparent)));

                      【讨论】:

                        【解决方案21】:

                        确保R.layout.themechanger 没有背景颜色,因为默认情况下对话框具有默认背景颜色。

                        您还需要添加dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

                        最后

                        <style name="TransparentDialog">
                            <item name="android:windowIsFloating">true</item>
                            <item name="android:windowNoTitle">true</item>
                            <item name="android:windowBackground">@android:color/transparent</item>
                            <item name="android:windowContentOverlay">@null</item>
                            <item name="android:windowTitleStyle">@null</item>
                        </style>
                        

                        【讨论】:

                          【解决方案22】:

                          如果您使用 Kotlin,此代码可以帮助您:

                          Objects.requireNonNull(dialog.window)
                                          ?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                          

                          【讨论】:

                            【解决方案23】:

                            在我的例子中,应用透明背景没有任何效果。

                            只有我在我的对话框中使用了 onStart():

                            dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                            

                            但它没有任何效果。我检查了styles.xml,没有与我的问题相关联。

                            最后,当我检查我的对话框是如何创建的时,我发现导航组件在我请求对话框片段时正在创建对话框。

                            在 navgraph.xml 的 XML 中,我将对话框片段定义为片段,因此,它被创建为片段而不是对话框。将该片段更改为对话框使一切都到位。

                            顺便说一句:您不能在导航编辑器的 GUI 中从片段修改为对话框。您应该手动更改代码。

                            这可能是对对话框的某些影响未反映在运行时的原因之一。

                            【讨论】:

                              【解决方案24】:

                              Kotlin 创建透明背景对话框的方法:

                               Dialog(activity!!, R.style.LoadingIndicatorDialogStyle)
                                          .apply {
                                              // requestWindowFeature(Window.FEATURE_NO_TITLE)
                                              setCancelable(true)
                                              setContentView(R.layout.define_your_custom_view_id_here)
                              
                                              //access your custom view buttons/editText like below.z
                                              val createBt = findViewById<TextView>(R.id.clipboard_create_project)
                                              val cancelBt = findViewById<TextView>(R.id.clipboard_cancel_project)
                                              val clipboard_et = findViewById<TextView>(R.id.clipboard_et)
                                              val manualOption =
                                                  findViewById<TextView>(R.id.clipboard_manual_add_project_option)
                              
                                              //if you want to perform any operation on the button do like this
                              
                                              createBt.setOnClickListener {
                                                  //handle your button click here
                                                  val enteredData = clipboard_et.text.toString()
                                                  if (enteredData.isEmpty()) {
                                                      Utils.toast("Enter project details")
                                                  } else {
                                                      navigateToAddProject(enteredData, true)
                                                      dismiss()
                                                  }
                                              }
                              
                                              cancelBt.setOnClickListener {
                                                  dismiss()
                                              }
                                              manualOption.setOnClickListener {
                                                  navigateToAddProject("", false)
                                                  dismiss()
                                              }
                                              show()
                                          }
                              

                              像这样在 style.xml 中创建 LoadingIndicatorDialogStyle:

                              <style name="LoadingIndicatorDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
                                  <item name="android:windowIsTranslucent">true</item>
                                  <item name="android:windowBackground">@android:color/transparent</item>
                                  <item name="android:windowContentOverlay">@null</item>
                                  <item name="android:windowNoTitle">true</item>
                                  <item name="android:statusBarColor">@color/black_transperant</item>
                                  <item name="android:layout_gravity">center</item>
                                  <item name="android:background">@android:color/transparent</item>
                                  <!--<item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>-->
                              </style>
                              

                              【讨论】:

                                猜你喜欢
                                • 1970-01-01
                                • 1970-01-01
                                • 2023-03-29
                                • 2022-01-23
                                • 2015-07-17
                                • 1970-01-01
                                • 1970-01-01
                                • 2018-05-30
                                相关资源
                                最近更新 更多