如何更改 Dialog 的默认黑色暗淡背景 "color"(不是暗淡的数量)?
How can I change default black dim background "color" (not the amount of dim) of Dialog?
(这是在互联网上找到的 Dialog
的随机图像。)
我一直在实施自定义 Dialog
。我可以处理对话框上的几乎所有内容,除了对话框本身下方的默认黑色暗淡背景,但在其后面的整个屏幕上。基本上我想改变它的 color 和 alpha 值。
我一直在 Whosebug 上徘徊,但我找到的唯一答案是关于改变 Dialog
本身的背景。无论如何,如果你需要它,这是我的简单代码。
CustomDialog.java
public class HTDialog extends Dialog{
public HTDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setCanceledOnTouchOutside(true);
setContentView(R.layout.custom_dialog);
}
}
custom_dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="280dp"
android:background="@drawable/bg_popup"
android:paddingTop="20dp">
<ImageView
android:id="@+id/dialog_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/icon" />
</RelativeLayout>
使用自定义样式。
<style name="transparent_dialog_borderless" parent="android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">#FF333333</item>
<item name="android:windowBackground">@null</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
android:backgroundDimEnabled:控制黑色暗淡背景
尝试代码,
View checkBoxView = View.inflate(context, R.layout.alertbox, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(checkBoxView);
builder.setCancelable(false);
Dialog d = builder.create();
d.getWindow().setBackgroundDrawable(new ColorDrawable(0));
d.setView(checkBoxView, 0, 0, 0, 0);
d.show();
nb:行 d.setView(checkBoxView, 0, 0, 0, 0);会成功的...
尝试设置对话框的样式windows,
示例:
Dialog dialog = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);
这是一种解决方法,但它并不是真正的纯粹解决方案,因为背景触摸已禁用,应手动配置。
首先,像这样设置自定义对话框主题。
styles.xml
<style name="CustomDialogTheme" parent="android:Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
将 windowIsFloating
设置为 false 强制 Dialog
视图扩展为全屏。将 windowBackground
设置为 transparent
会移除 Dialog
下的默认黑色暗淡背景。 windowNoTitle
选项去掉了上方的标题栏。
CustomDialog.java
应用主题并构建您的 custom_dialog
视图,如下所示。
public HTCustomDialog(Context context) {
super(context, R.style.CustomDialogTheme);
setContentView(R.layout.custom_dialog);
}
custom_dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_solid_80">
<RelativeLayout
android:id="@+id/dialog_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="@drawable/bg_popup"
android:padding="16dp">
</RelativeLayout>
现在 CustomDialog
视图是 full-screen 视图,将根布局的 background
设置为您喜欢的任何颜色。
示例结果
我对结果做了一点马赛克。
这对我有用:
val dialog = AlertDialog.Builder(context)
.setView(view)
.setCancelable(true)
.setPositiveButton(R.string.done_label, { dialog, _ -> dialog.dismiss() })
.create()
dialog.window.setDimAmount(0f)
dialog.show()
dialog.window.setDimAmount(0f)
是关键。
下面的自定义 DatePickerDoalog class 不仅可以自定义暗淡的颜色,还可以使暗淡的动画显示
/**
* @author Taras Yurkiv @Devlight
*/
public class DatePickerDialogCustomDim extends DatePickerDialog {
private final long animDuration = 100;
private float dimAmount = 0.7f;
private Drawable dimDrawable;
private ViewGroup root;
private OnDismissListener outsideDismissListener;
private final OnDismissListener dismissListener = new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
final ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(dimDrawable,
PropertyValuesHolder.ofInt("alpha", (int) (255 * dimAmount), 0));
animator.setTarget(dimDrawable);
animator.setDuration(animDuration);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
ViewGroupOverlay overlay = root.getOverlay();
overlay.remove(dimDrawable);
}
});
animator.start();
if (outsideDismissListener != null)
outsideDismissListener.onDismiss(dialog);
}
};
@TargetApi(Build.VERSION_CODES.N)
public DatePickerDialogCustomDim(@NonNull Context context) {
this(context, 0);
}
@TargetApi(Build.VERSION_CODES.N)
public DatePickerDialogCustomDim(@NonNull Context context, @StyleRes int themeResId) {
this(context, themeResId, null, -1, -1, -1);
init(context);
}
public DatePickerDialogCustomDim(@NonNull Context context,
@Nullable OnDateSetListener listener,
int year,
int month,
int dayOfMonth) {
this(context, 0, listener, year, month, dayOfMonth);
}
public DatePickerDialogCustomDim(@NonNull Context context,
@StyleRes int themeResId,
@Nullable OnDateSetListener listener,
int year,
int monthOfYear,
int dayOfMonth) {
super(context, themeResId, listener, year, monthOfYear, dayOfMonth);
init(context);
}
private void init(Context context) {
root = ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content);
super.setOnDismissListener(dismissListener);
}
public void setDimAmount(@FloatRange(from = 0, to = 1f) float dim) {
dimAmount = dim;
}
@Override
public void show() {
super.show();
dimDrawable = new ColorDrawable(Color.WHITE); // a dim color
dimDrawable.setBounds(0, 0, root.getWidth(), root.getHeight());
ViewGroupOverlay overlay = root.getOverlay();
overlay.add(dimDrawable);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(dimDrawable,
PropertyValuesHolder.ofInt("alpha", 0, (int) (255 * dimAmount)));
animator.setTarget(dimDrawable);
animator.setDuration(animDuration);
animator.start();
}
@Override
public void setOnDismissListener(@Nullable OnDismissListener listener) {
outsideDismissListener = listener;
}
}
它与一种风格结合使用
<style name="DatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/accent</item>
<item name="android:textColorLink">@color/primary</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
为 backgroundDimAmount 设置一个介于 0-1 之间的浮点值(它是一个浮点值)
<style name="DarkTransparentBgDialog" parent="@android:style/Theme.Dialog">
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.87</item>
</style>
在 DialogFragment 中(在 Kotlin 中)
override fun getTheme() = R.style.DarkTransparentBgDialog
(这是在互联网上找到的 Dialog
的随机图像。)
我一直在实施自定义 Dialog
。我可以处理对话框上的几乎所有内容,除了对话框本身下方的默认黑色暗淡背景,但在其后面的整个屏幕上。基本上我想改变它的 color 和 alpha 值。
我一直在 Whosebug 上徘徊,但我找到的唯一答案是关于改变 Dialog
本身的背景。无论如何,如果你需要它,这是我的简单代码。
CustomDialog.java
public class HTDialog extends Dialog{
public HTDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setCanceledOnTouchOutside(true);
setContentView(R.layout.custom_dialog);
}
}
custom_dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="280dp"
android:background="@drawable/bg_popup"
android:paddingTop="20dp">
<ImageView
android:id="@+id/dialog_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/icon" />
</RelativeLayout>
使用自定义样式。
<style name="transparent_dialog_borderless" parent="android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">#FF333333</item>
<item name="android:windowBackground">@null</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
android:backgroundDimEnabled:控制黑色暗淡背景
尝试代码,
View checkBoxView = View.inflate(context, R.layout.alertbox, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(checkBoxView);
builder.setCancelable(false);
Dialog d = builder.create();
d.getWindow().setBackgroundDrawable(new ColorDrawable(0));
d.setView(checkBoxView, 0, 0, 0, 0);
d.show();
nb:行 d.setView(checkBoxView, 0, 0, 0, 0);会成功的...
尝试设置对话框的样式windows,
示例:
Dialog dialog = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);
这是一种解决方法,但它并不是真正的纯粹解决方案,因为背景触摸已禁用,应手动配置。
首先,像这样设置自定义对话框主题。
styles.xml
<style name="CustomDialogTheme" parent="android:Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
将 windowIsFloating
设置为 false 强制 Dialog
视图扩展为全屏。将 windowBackground
设置为 transparent
会移除 Dialog
下的默认黑色暗淡背景。 windowNoTitle
选项去掉了上方的标题栏。
CustomDialog.java
应用主题并构建您的 custom_dialog
视图,如下所示。
public HTCustomDialog(Context context) {
super(context, R.style.CustomDialogTheme);
setContentView(R.layout.custom_dialog);
}
custom_dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_solid_80">
<RelativeLayout
android:id="@+id/dialog_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="@drawable/bg_popup"
android:padding="16dp">
</RelativeLayout>
现在 CustomDialog
视图是 full-screen 视图,将根布局的 background
设置为您喜欢的任何颜色。
示例结果
我对结果做了一点马赛克。
这对我有用:
val dialog = AlertDialog.Builder(context)
.setView(view)
.setCancelable(true)
.setPositiveButton(R.string.done_label, { dialog, _ -> dialog.dismiss() })
.create()
dialog.window.setDimAmount(0f)
dialog.show()
dialog.window.setDimAmount(0f)
是关键。
下面的自定义 DatePickerDoalog class 不仅可以自定义暗淡的颜色,还可以使暗淡的动画显示
/**
* @author Taras Yurkiv @Devlight
*/
public class DatePickerDialogCustomDim extends DatePickerDialog {
private final long animDuration = 100;
private float dimAmount = 0.7f;
private Drawable dimDrawable;
private ViewGroup root;
private OnDismissListener outsideDismissListener;
private final OnDismissListener dismissListener = new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
final ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(dimDrawable,
PropertyValuesHolder.ofInt("alpha", (int) (255 * dimAmount), 0));
animator.setTarget(dimDrawable);
animator.setDuration(animDuration);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
ViewGroupOverlay overlay = root.getOverlay();
overlay.remove(dimDrawable);
}
});
animator.start();
if (outsideDismissListener != null)
outsideDismissListener.onDismiss(dialog);
}
};
@TargetApi(Build.VERSION_CODES.N)
public DatePickerDialogCustomDim(@NonNull Context context) {
this(context, 0);
}
@TargetApi(Build.VERSION_CODES.N)
public DatePickerDialogCustomDim(@NonNull Context context, @StyleRes int themeResId) {
this(context, themeResId, null, -1, -1, -1);
init(context);
}
public DatePickerDialogCustomDim(@NonNull Context context,
@Nullable OnDateSetListener listener,
int year,
int month,
int dayOfMonth) {
this(context, 0, listener, year, month, dayOfMonth);
}
public DatePickerDialogCustomDim(@NonNull Context context,
@StyleRes int themeResId,
@Nullable OnDateSetListener listener,
int year,
int monthOfYear,
int dayOfMonth) {
super(context, themeResId, listener, year, monthOfYear, dayOfMonth);
init(context);
}
private void init(Context context) {
root = ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content);
super.setOnDismissListener(dismissListener);
}
public void setDimAmount(@FloatRange(from = 0, to = 1f) float dim) {
dimAmount = dim;
}
@Override
public void show() {
super.show();
dimDrawable = new ColorDrawable(Color.WHITE); // a dim color
dimDrawable.setBounds(0, 0, root.getWidth(), root.getHeight());
ViewGroupOverlay overlay = root.getOverlay();
overlay.add(dimDrawable);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(dimDrawable,
PropertyValuesHolder.ofInt("alpha", 0, (int) (255 * dimAmount)));
animator.setTarget(dimDrawable);
animator.setDuration(animDuration);
animator.start();
}
@Override
public void setOnDismissListener(@Nullable OnDismissListener listener) {
outsideDismissListener = listener;
}
}
它与一种风格结合使用
<style name="DatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/accent</item>
<item name="android:textColorLink">@color/primary</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
为 backgroundDimAmount 设置一个介于 0-1 之间的浮点值(它是一个浮点值)
<style name="DarkTransparentBgDialog" parent="@android:style/Theme.Dialog">
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.87</item>
</style>
在 DialogFragment 中(在 Kotlin 中)
override fun getTheme() = R.style.DarkTransparentBgDialog