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

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

问题描述

我有一个主要的活动,用户可以通过偏好将背景颜色更改为最喜欢的颜色。我的问题是我无法更改任何自定义对话框的背景颜色。



堆栈溢出中的其他答案建议:



(a)



我按照这篇文章解决了我自定义对话框的问题。



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



谢谢。


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天全站免登陆