[英]Dialog with transparent background in Android
如何從 Android 中的對話框中刪除黑色背景。圖片顯示了問題。
final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger);
添加此代碼
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
或者這個:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
TL;博士; 您只需要兩件事,首先在您的style
中執行以下操作:
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
其次,確保 100% 確定將上述style
應用於您的對話框(可能通過傳遞給構造函數)。
<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);
希望對你有幫助!
我遇到了更簡單的問題,我想出的解決方案是應用透明的背景主題。 用你的風格寫下這些行
<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"
不知何故,撒迦利亞的解決方案對我不起作用,所以我使用下面的主題來解決這個問題......
<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);
享受!!
如果你想破壞對話框的深色背景,使用這個
dialog.getWindow().setDimAmount(0);
您可以使用:
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
*/
對話框彈出填充默認黑色背景顏色或主題顏色,因此您需要將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();
我在所有現有答案中發現的一個問題是沒有保留邊距。 這是因為它們都用純色覆蓋了負責邊距的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);
然后像往常一樣構建、創建和顯示對話框!
注意:不要使用生成器來改變背景。
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()
選項。
在你的代碼中試試這個:
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
它肯定會工作......在我的情況下......! 我的朋友
與 zGnep 相同的解決方案,但使用 xml:
android:background="@null"
這就是我為使用 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();
您可以使用(可選)
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
我建議創建一個擴展函數。 像extensions.kt
這樣的東西
import android.app.Dialog
fun Dialog.setTransparentBackground() {
window?.setBackgroundDrawableResource(android.R.color.transparent)
}
並在任何對話框中使用它
dialog.setTransparentBackground()
有一些有趣的編程...
使用此代碼,它適用於我:
Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
dialog.show();
在我的情況下,解決方案是這樣的:
dialog_AssignTag.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
此外,在自定義對話框的 XML 中:
android:alpha="0.8"
如果您擴展了DialogFrament
類,您可以使用以下方式設置主題:
setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialogTheme);
然后在您的 styles.xml 文件中創建自定義主題(請參閱@LongLv 的參數答案)
如果您希望在用戶觸摸對話框外部時關閉對話框,請不要忘記添加<item name="android:windowCloseOnTouchOutside">true</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>
只需將 false 更改為 true 即可
<item name="android:backgroundDimEnabled">true</item>
它會使你的背景變暗。
Window window = d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
這是我的方法,你可以試試!
對於使用帶有自定義類的自定義對話框的任何人,您需要更改類中的透明度,在 onCreate() 中添加以下行:
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(ctx, android.R.color.transparent)));
確保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>
如果您使用Kotlin ,此代碼可以幫助您:
Objects.requireNonNull(dialog.window)
?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
就我而言,沒有任何方法可以應用透明背景。
只有我在我的對話框 onStart() 中使用過:
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
但它沒有任何效果。 我檢查了styles.xml,沒有與我的問題相關聯。
最后,當我檢查我的對話框是如何創建的時,我發現導航組件在我請求對話框片段時正在創建對話框。
在 navgraph.xml 的 XML 中,我將對話框片段定義為片段,因此,它被創建為片段而不是對話框。 將該片段更改為對話框使一切都到位。
順便說一句:您不能在導航編輯器的 GUI 中從片段修改為對話框。 您應該手動更改代碼。
這可能是對對話框的某些影響未反映在運行時的原因之一。
在 Kotlin 你可以使用這個代碼:
var dialog=Dialog(context)
dialog.window!!.decorView.background=null
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.