如何从Android对话框中删除黑色背景。这张图片显示了问题所在。
final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger);
如何从Android对话框中删除黑色背景。这张图片显示了问题所在。
final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger);
当前回答
对话框弹出填充默认的黑色背景颜色或主题颜色,所以你需要设置透明的背景到对话框。试试下面的代码:-
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();
其他回答
与zGnep相同的解决方案,但使用xml:
android:background="@null"
我在所有现有答案中发现的一个问题是,边际没有得到保留。这是因为它们都覆盖了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>
确保用刚刚创建的可绘制文件的实际名称替换新婚created_background_name,并用所需的透明背景替换some_transparent_color。
之后我们要做的就是确定主题。在创建AlertDialog时使用此选项。建造者:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyTransparentDialog);
然后像往常一样构建、创建和显示对话框!
对于任何使用自定义类的自定义对话框的人,你需要改变类中的透明度,在onCreate()中添加这一行:
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
不知何故,撒迦利亚的解决方案对我不起作用,所以我用下面的主题来解决这个问题…
<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);
享受! !
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>