How can I change default black dim background quot;colorquot; (not the amount of dim) of Dialog?(如何更改默认的黑色暗淡背景“颜色?(不是dim的量)Dialog?)
问题描述
(这是一张随机图片,显示了在 Internet 上找到的 Dialog
.)
(This is a random image of showing a Dialog
found on the Internet.)
我一直在实现一个自定义 Dialog
.我可以处理对话框上的几乎所有内容,除了对话框本身下的默认黑色暗背景,但在它后面的整个屏幕上.基本上我想改变它的 color 和 alpha 值.
I've been implementing a custom Dialog
. I could handle almost everything on the dialog except for that default black dim background under the dialog itself, but over the entire screen behind it. Basically I want to change its color and alpha value.
我一直在 StackOverflow 中徘徊,但我找到的唯一答案是关于更改 Dialog
本身的背景.无论如何,如果您需要它,这是我的简单代码.
I've been wandering through StackOverflow but only answers I found were about changing background of Dialog
itself. In any case if you need it, here's my simple codes.
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="QGRyYXdhYmxlL2ljb24=" />
</RelativeLayout>
推荐答案
这是一种变通方法,但它并不是真正的纯粹解决方案,因为背景触摸已禁用,应手动配置.
This is a workaround but it's not really a pure solution because background touch is disabled and should be configured manually.
首先,像这样设置自定义对话框主题.
First, set custom dialog theme like this.
<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
选项去掉上方的标题栏.
Setting windowIsFloating
to false forces Dialog
view to be expanded to full screen. Setting windowBackground
to transparent
removes default black dim background under Dialog
. windowNoTitle
option gets rid of the upper title bar.
应用主题并构建您的 custom_dialog
视图,如下所示.
Apply the theme and construct your custom_dialog
view as follows.
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
视图是全屏视图,请将根布局的 background
设置为您喜欢的任何颜色.
Now that CustomDialog
view is a full-screen view, set background
of your root layout to whatever color you'd like.
我将结果镶嵌了一点.
这篇关于如何更改默认的黑色暗淡背景“颜色"?(不是dim的量)Dialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何更改默认的黑色暗淡背景“颜色"?(不是dim的量)Dialog?


- Android - 拆分 Drawable 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01