简体   繁体   English

为对话框中的按钮设置背景?

[英]Set a background to buttons in a dialog?

I have a dialog, which works just fine for me, but I want to set my background for two buttons in this dialog.我有一个对话框,对我来说效果很好,但我想为这个对话框中的两个按钮设置我的背景。 The structure of it is quite complicated so I don't want to rewrite it to a custom dialog.它的结构相当复杂,所以我不想将它重写为自定义对话框。 But in this case, is there any possibility to set a background(more specifically, is there a way to set a style to positive/negative/neutral buttons)?但是在这种情况下,是否有可能设置背景(更具体地说,有没有办法将样式设置为正/负/中性按钮)?

Fundamentally you want to access the dialog buttons: these (on the standard AlertDialog) currently have ids android.R.id.button1 for positive, android.R.id.button2 for negative, and android.R.id.button3 for neutral. Fundamentally you want to access the dialog buttons: these (on the standard AlertDialog) currently have ids android.R.id.button1 for positive, android.R.id.button2 for negative, and android.R.id.button3 for neutral.

So for example to set the background image on the neutral button you can do this:因此,例如要在中性按钮上设置背景图像,您可以这样做:

Dialog d;
//
// create your dialog into the variable d
//
((Button)d.findViewById(android.R.id.button3)).setBackgroundResource(R.drawable.new_background);

EDIT: this is if you are using an AlertDialog.Builder to create it.编辑:这是如果您使用 AlertDialog.Builder 来创建它。 For all I know these button assignments may change in the future, so keep that in mind.据我所知,这些按钮分配将来可能会改变,所以请记住这一点。

EDIT: The chunk of code below should generate something that looks like what you want.编辑:下面的代码块应该生成看起来像你想要的东西。 It turns out you have to call show BEFORE you change the background事实证明,您必须在更改背景之前调用 show

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("TEST MESSAGE)
        .setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        })
        .setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
((Button)alert.findViewById(android.R.id.button1)).setBackgroundResource(R.drawable.button_border);

Actually there's a better and more reliable way to get the buttons for the Dialog than the one posted by @Femi.实际上,有一种比@Femi 发布的方法更好、更可靠的方法来获取Dialog的按钮。

You can use the getButton method:您可以使用getButton方法:

Button positiveButton = yourDialog.getButton(DialogInterface.BUTTON_POSITIVE);

The DialogInterface provides all the needed constants: DialogInterface提供了所有需要的常量:

DialogInterface.BUTTON_POSITIVE
DialogInterface.BUTTON_NEUTRAL
DialogInterface.BUTTON_NEGATIVE

Please let me note that this would be bad practice to override the show() method for such problem.请让我注意,对于此类问题,覆盖 show() 方法将是不好的做法。

In order to encapsulate the solution into the creation of the dialog itself, best practice is to have the buttons customization within dialog constructor:为了将解决方案封装到对话框本身的创建中,最佳实践是在对话框构造函数中自定义按钮:

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

        setOnShowListener(new OnShowListener()
        {
            @Override
            public void onShow(DialogInterface dialog)
            {
                Button negativeButton = getButton(DialogInterface.BUTTON_NEGATIVE);  
                Button positiveButton = getButton(DialogInterface.BUTTON_POSITIVE);

                negativeButton.setBackgroundColor(Color.GREEN);
                positiveButton.setBackgroundColor(Color.RED);
            }
        }
    }
}
AlertDialog alert = builder.create();
alert.show();
Button bn = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
bn.setBackgroundColor(Color.RED);
Button bp = alert.getButton(DialogInterface.BUTTON_POSITIVE);
bp.setBackgroundColor(Color.YELLOW);

If you want to keep the standard text buttons and change the background of the buttons and the whole dialog you can define your own dialog theme in styles.xml like this:如果您想保留标准文本按钮并更改按钮和整个对话框的背景,您可以在styles.xml中定义自己的对话框主题,如下所示:

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:background">@color/colorBackground</item>
</style>

...and then when you build the dialog to set its background like this: ...然后当您构建对话框以设置其背景时,如下所示:

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyDialogTheme);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM