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="@drawable/icon" />
</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?
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
