gpt4 book ai didi

android - 如何更改 Dialog 的默认黑色暗淡背景 "color"(不是暗淡的数量)?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:20:58 33 4
gpt4 key购买 nike

enter image description here

(这是在 Internet 上找到的显示 Dialog 的随机图像。)

我一直在实现自定义 Dialog。我可以处理对话框上的几乎所有内容,除了对话框本身下方的默认黑色暗淡背景,但在其后面的整个屏幕上。基本上我想改变它的颜色和alpha值。

我一直在 StackOverflow 中徘徊,但我找到的唯一答案是关于改变 Dialog 本身的背景。无论如何,如果您需要它,这是我的简单代码。

自定义对话框.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);
}
}

自定义对话框.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>

最佳答案

这是一种解决方法,但它并不是真正的纯粹解决方案,因为背景触摸已禁用,应手动配置。

首先,像这样设置自定义对话框主题。

样式.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 View 扩展到全屏。将 windowBackground 设置为 transparent 会移除 Dialog 下的默认黑色昏暗背景。 windowNoTitle 选项去掉了上方的标题栏。

自定义对话框.java

应用主题并构建您的 custom_dialog View ,如下所示。

public HTCustomDialog(Context context) {
super(context, R.style.CustomDialogTheme);
setContentView(R.layout.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 View 是全屏 View ,将根布局的 background 设置为您喜欢的任何颜色。

示例结果

我对结果进行了一些拼接。

Result

关于android - 如何更改 Dialog 的默认黑色暗淡背景 "color"(不是暗淡的数量)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29227126/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com