自定义透明对话框(windowBackground不存在的?) [英] Custom Transparent Dialog (windowBackground non-existant?)

查看:1796
本文介绍了自定义透明对话框(windowBackground不存在的?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个半透明的背景自定义对话框。我设法得到它通过code一起工作:

  getWindow()setBackgroundDrawableResource(R.color.bg_tran)。

在哪里bg_tran ==#A0000000。不过,我宁愿有它在XML作为我用这几个不同的自定义对话框类。只需使用的android:背景不起作用。也设置主题@android:风格/ Theme.Translucent工作

我发现了几个文章谈论使用属性android:windowBackground。然而,Android的:windowBackground属性不存在。含义Eclipse不告诉我它作为自动完成选项。我已经在我的样式表和我的实际layout.xml内检查。由于我编译2.2我觉得这个问题,改变建设目标4.0.3。没有修复。我甚至试过明确使用也无妨,但它不工作。我找遍了Android开发网站,甚至找不到描述该属性页。除了在传球偶尔提及,没有什么。此外仰视setBackgroundDrawableResource并没有告诉我,相当于XML属性标记是什么。我极其困惑。我缺少什么?

堆栈溢出引用我用上述学习:

<一href=\"http://stackoverflow.com/questions/6070505/android-how-to-create-a-transparent-dialog-themed-activity\">Transparent对话主题

Android的对话框透明

如何创建一个自定义对话框透明

更新:

如果它是不明确的,是的,我已经试过一个主题中设置此功能。不工作。这里是我定义的主题:

 &LT;样式名称=my_dialog_theme父=@安卓风格/ Theme.Dialog&GT;
    &LT;项目名称=机器人:windowBackground&GT; @彩色/ bg_tran&LT; /项目&GT;
    &LT;项目名称=机器人:windowIsTranslucent&GT;真&LT; /项目&GT;
    &LT;项目名称=机器人:layout_height&GT;&WRAP_CONTENT LT; /项目&GT;
    &LT;项目名称=机器人:layout_width&GT;&WRAP_CONTENT LT; /项目&GT;
&LT; /风格&GT;


解决方案

您需要定义一个自定义的主题为你的对话。

 &LT;样式名称=Theme.CustomDialog父=机器人:Theme.Dialog&GT;
    &LT;项目名称=机器人:windowIsTranslucent&GT;真&LT; /项目&GT;
    &LT;项目名称=机器人:windowBackground&GT; @android:彩色/透明&LT; /项目&GT;
    ...
    ...
&LT; /风格&GT;

更新:

您可以实现您的对话框通过延长对话框类想要什么。在这个派生类中设置窗口背景绘制。

 公共类CustomDialog扩展对话框
{
    公共CustomDialog(最终上下文的背景下)
    {
        超级(上下文);        //这是描述你的对话框布局的布局XML文件
        this.setContentView(R.layout.myDialogLayout);
        。getWindow()setBackgroundDrawableResource(R.color.bg_tran);
    }
}

I'm attempting to create a custom dialog with a semi transparent background. I managed to get it to work through code with:

getWindow().setBackgroundDrawableResource(R.color.bg_tran);

Where bg_tran == #A0000000. However I'd rather have it in XML as I use this for several different custom dialog classes. Using just android:background does not work. Nor setting the theme to @android:style/Theme.Translucent worked.

I found several articles talking about using the attribute android:windowBackground. However the android:windowBackground attribute does not exist. Meaning eclipse doesn't show me it as an option for auto complete. I've checked within my style sheet and my actual layout.xml. Since I'm compiling for 2.2 I thought that the issue and changed build target to 4.0.3. No fix. I've even tried explicitly using it anyway but it doesn't work. I searched the Android Dev website and can't even find the page which describes this attribute. Aside from occasionally mentioning in passing, there's nothing. Also looking up setBackgroundDrawableResource doesn't tell me what the equivalent XML attribute tag is. I'm incredibly confused. What am I missing?

Stack overflow references I used to learn above:
Transparent Dialog Theme
Android Dialog Transparent
How to make a custom dialog transparent

Update:
In case it wasn't clear, yes I've tried setting this up within a theme. Does not work. Here's my defined theme:

<style name="my_dialog_theme" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@color/bg_tran</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>
</style>

解决方案

You need to define a custom theme for your dialog.

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

Update:

You can achieve what you want in your dialog by extending the Dialog class. Set the window background drawable in this derived class.

public class CustomDialog extends Dialog 
{
    public CustomDialog (final Context context)
    {
        super(context);

        // This is the layout XML file that describes your Dialog layout
        this.setContentView(R.layout.myDialogLayout);  
        getWindow().setBackgroundDrawableResource(R.color.bg_tran);
    }
}

这篇关于自定义透明对话框(windowBackground不存在的?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆