如何以编程方式更改对话框背景颜色?

Mel*_*Mel 18 android dialog background-color

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

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

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

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

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

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

代码包含在图像之后,有关如何更改对话框背景的建议将不胜感激.

对话外观

默认外观的代码

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");
}
Run Code Online (Sandbox Code Playgroud)

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

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

}
Run Code Online (Sandbox Code Playgroud)

小智 17

我找到了一个规则解决方案!

d.getWindow().setBackgroundDrawableResource(R.drawable.menubackground);
Run Code Online (Sandbox Code Playgroud)

它适用于我的正常对话框.但我不知道它是否适用于AlertDialog.

  • 我刚尝试过,但事实并非如此.我试着用纯色和我的颜色而不是背景中的alertdialog的阴影. (2认同)

rog*_*gcg 8

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

这篇帖子给了我很多帮助.http://blog.androgames.net/10/custom-android-dialog/

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

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

谢谢.


act*_*e93 5

我发现了一个骇客,无需创建自定义布局,您就可以通过玩的某些属性来创建多个设计AlertDialog

您要做的是:

AlertDialog在屏幕上可见时,将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();
    }
});
Run Code Online (Sandbox Code Playgroud)


S H*_*ini 5

对话背景色

dialog.getWindow().setBackgroundDrawableResource(R.color.glassblack);
Run Code Online (Sandbox Code Playgroud)

对我来说真的很好。