如何以编程方式更改对话框的背景颜色? [英] How to change dialog background color programatically?

查看:314
本文介绍了如何以编程方式更改对话框的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要活动,用户可以更改(通过preferences)的背景色为自己喜欢的颜色。我的问题是,我不能改变任何自定义对话框的背景颜色。

在堆栈溢出其他答案提示:

(一) overiding默认主题到preferred颜色。我不认为在这种情况下,一个合适的解决方案,因为我知道,改变主题在运行时不推荐使用。

(二)改变在styles.xml 的(不适合在这种情况下,作为我不能在运行时更改)

(三)重写AlertBuilder类(但这种色调,整个警报对话框)

这最接近我是来改变颜色被删除警报建设者称号,并自定义视图的背景设置为喜欢的颜色(eg.pink)。很不幸,这给出了一个丑陋的带在对话框的顶部和底部。

code包括图像之后,有关如何更改对话框的背景建议将不胜AP preciated。

$ C $下的默认外观

 保护对话框onCreateDialog(INT dialogId){
    LayoutInflater充气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    最后AlertDialog.Builder建设者=新AlertDialog.Builder(本);


        最后查看viewMessEdit = inflater.inflate(R.layout.example,(ViewGroup中)findViewById(R.id.dialog_mess_edit_root));
              builder.setView(viewMessEdit);
        builder.setTitle(警报建设者的标题);
}
 

code更改自定义对话框意见的背景颜色(和警报建设者的标题被删除)

 保护对话框onCreateDialog(INT dialogId){
    LayoutInflater充气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    最后AlertDialog.Builder建设者=新AlertDialog.Builder(本);


        最后查看viewMessEdit = inflater.inflate(R.layout.example,(ViewGroup中)findViewById(R.id.dialog_mess_edit_root));
              builder.setView(viewMessEdit);
              viewMessEdit.setBackgroundResource(R.color.pink_dark);

}
 

解决方案

我还挺面临同样的问题。而唯一的方式来解决它延长我自己的版本的布局。我看到,在你的情况下,它是一个AlertDialog。我建议你​​做的,是创造一类独特的,那是你的自定义AlertDialog并为此创造一个布局,然后你夸大这一点。

下面是一个职位,对我帮助很大。 <一href="http://web.archive.org/web/20120203153326/http://blog.androgames.net/10/custom-android-dialog/">http://blog.androgames.net/10/custom-android-dialog/

我跟着这篇文章,与自定义对话框解决我的问题。

请,如果您有更多的疑问,让我知道。

感谢。

I have a main activity where users can change (via preferences) the background colour to their favourite colour. My problem is that I can not change the background colour of of any custom dialogs.

Other answers in stack overflow suggest:

(a) overiding the default theme to the preferred colour. I don't think is a suitable solution in this case as I know changing themes at run time is not recommended.

(b) Changing in styles.xml (not suitable in this case as I can't change at run time)

(c) Overriding the AlertBuilder class (but this tints the whole alert dialog)

This closest I have come to changing the colour is removing the alert builder title, and setting the custom view's background to the favourite colour (eg.pink). Unfortunately this gives an ugly strip at the top and bottom of the dialog.

Code is included after image, suggestions about how to change dialog background would be greatly appreciated.

Code for the default appearance

protected Dialog onCreateDialog(int dialogId) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);


        final View viewMessEdit = inflater.inflate(R.layout.example,(ViewGroup) findViewById(R.id.dialog_mess_edit_root));
              builder.setView(viewMessEdit);
        builder.setTitle("Alert builder's title");
}

Code for changing the custom dialog views background colour (and the Alert builder's title is removed)

protected Dialog onCreateDialog(int dialogId) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);


        final View viewMessEdit = inflater.inflate(R.layout.example,(ViewGroup) findViewById(R.id.dialog_mess_edit_root));
              builder.setView(viewMessEdit);
              viewMessEdit.setBackgroundResource(R.color.pink_dark);

}

解决方案

I kinda faced the same problem. And the only way to solve it was extending my own version of layout. I see that in your case it is an AlertDialog. What I recommend you to do, is to create a unique class, that is your customized AlertDialog and create a layout for this, and then you inflate this.

Here is a post that helped me a lot. http://blog.androgames.net/10/custom-android-dialog/

I followed this post and solved my problem with customizing dialogs.

Please, if you have more doubts, let me know.

Thanks.

这篇关于如何以编程方式更改对话框的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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