Android对话框按钮颜色主题,如何以编程方式更改对话框背景颜色?

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

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

(a) 将默认主题覆盖为首选颜色。 我不认为在这种情况下是一个合适的解决方案,因为我知道不建议在运行时更改主题。

(b) 更改styles.xml (在这种情况下不适合,因为我无法在运行时更改)

(c) 覆盖AlertBuilder类 (但这会使整个警报对话框着色)

我最近改变颜色的方法是删除警报构建器标题,并将自定义视图的背景设置为喜欢的颜色(例如,粉红色)。 不幸的是,这会在对话框的顶部和底部产生一个丑陋的条带。

代码包含在图像之后,将非常感谢有关如何更改对话框背景的建议。

mLwJ4.jpg

默认外观的代码

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"); }

用于更改自定义对话框视图背景颜色的代码(并删除了“警报”构建器的标题)

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); }

我有点面临同样的问题。 解决它的唯一方法是扩展我自己的布局版本。 我看到你的情况是一个AlertDialog。 我建议你做的是创建一个独特的类,即你自定义的AlertDialog并为此创建一个布局,然后你膨胀它。

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

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

谢谢。

我发现了一个规则解决方案!

d.getWindow().setBackgroundDrawableResource(R.drawable.menubackground);

它适用于普通对话框。 但我不知道它是否适用于AlertDialog 。

我发现了一个hack,没有创建自定义布局,你可以通过使用AlertDialog一些属性来创建多个设计。

你要做什么:

当屏幕上显示OnShowListener将调用OnShowListener 。 因此,通过添加dialog.setOnShowListener(this)您将能够自定义AlertDialog 。

码:

// Create AlertDialog AlertDialog.Builder adb = new AlertDialog.Builder(context1); adb.setTitle(context1.getString(R.string.app_name)) .setMessage(message) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); AlertDialog dialog = adb.create(); // Make some UI changes for AlertDialog dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(final DialogInterface dialog) { // Add or create your own background drawable for AlertDialog window Window view = ((AlertDialog)dialog).getWindow(); view.setBackgroundDrawableResource(R.drawable.your_drawable); // Customize POSITIVE, NEGATIVE and NEUTRAL buttons. Button positiveButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE); positiveButton.setTextColor(context1.getResources().getColor(R.color.primaryColor)); positiveButton.setTypeface(Typeface.DEFAULT_BOLD); positiveButton.invalidate(); Button negativeButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEGATIVE); negativeButton.setTextColor(context1.getResources().getColor(R.color.primaryColor)); negativeButton.setTypeface(Typeface.DEFAULT_BOLD); negativeButton.invalidate(); Button neutralButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEUTRAL); neutralButton.setTextColor(context1.getResources().getColor(R.color.primaryColor)); neutralButton.setTypeface(Typeface.DEFAULT_BOLD); neutralButton.invalidate(); } });

对话背景颜色

dialog.getWindow().setBackgroundDrawableResource(R.color.glassblack);

对我来说真的很好。

建议的解决方案

d.getWindow().setBackgroundDrawableResource(R.drawable.menubackground);

不能正常工作(在6.0中测试),因为它会更改对话框阴影

这是我的解决方案:

public static void changeDialogBackground(final Dialog dialog,final int resId) { dialog.getWindow().getDecorView().setBackgroundResource(resId); dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(final DialogInterface dialog) { Window window = ((AlertDialog)dialog).getWindow(); window.getDecorView().setBackgroundResource(resId); } }); }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值