android - 为 AlertDialog 提供自定义或透明背景

标签 android android-layout android-manifest android-alertdialog

我曾经很难将 Activity 的背景变成透明。我会正确地将布局背景设置为#00000000,并在 onCreate 方法中正确设置getWindow().setBackgroundDrawable(new ColorDrawable(0))。然而,通过这两个更改,我总是会得到一个灰黑色的容器来保存我的布局。但后来我发现我需要编辑 list 中的 Activity 标记以添加 android:theme="@android:style/Theme.Holo.Dialog"。瞧!仅此而已。

但现在我需要将 AlertDialog 的背景变为透明。那里有很多建议,我已经尝试了很多。下面提供了我的最新配置。但我总是遇到与以前的 Activity 相同的问题:保存我的布局的灰黑色容器。所以现在,我的问题是:如何在自定义对话框的 list 文件中添加 android:theme="@android:style/Theme.Holo.Dialog"

当前代码:

public void showMyDialog() {
  ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.CustomDialog);
  AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
  LayoutInflater inflater = (LayoutInflater) ctw.getSystemService(LAYOUT_INFLATER_SERVICE);
  View view = inflater.inflate(R.layout.dialog_layout,
    (ViewGroup) findViewById(R.id.pr_root));
  builder.setView(view);
  builder.show();
}

风格:

<style name="CustomDialog" parent="android:Theme.Holo.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

<!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:alertDialogStyle">@style/CustomDialog</item>
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

当然dialog_layout是一个典型的布局.xml文件。

最佳答案

使用 Dialog 而不是 AlertDialog.Builder,因此使用 setContentView 而不是 setView。

关于android - 为 AlertDialog 提供自定义或透明背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17646054/

相关文章:

java - 单个应用程序有两个 list 文件?

Android EditText 透明背景

android - 改造 2.0 多部分

java - 两个不同 Activity 的两个不同背景会使应用程序崩溃

java - 找不到目标设备 android studio 2.1.1

android - 多次包含时在布局内设置Android RemoteViews的文本

android - LinearLayout 随机闪烁

android - 使用 Gradle 的多种口味的不同 list

Android edittext - 输入 "text selection mode"

如果从计时器任务调用,Android 对话框根本不显示