我曾经很难将 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/