我有一个对话框,对我来说工作得很好,但我想为这个对话框中的两个按钮设置我的背景。它的结构非常复杂,所以我不想将它重写为自定义对话框。但是在这种情况下,是否有可能设置背景(更具体地说,有没有办法将样式设置为正/负/中性按钮)?
最佳答案
基本上你想访问对话框按钮:这些(在标准的 AlertDialog 上)目前有 ids android.R.id.button1
代表积极,android.R.id.button2
表示否定,android.R.id.button3
表示中性。
因此,例如要在中性按钮上设置背景图像,您可以这样做:
Dialog d;
//
// create your dialog into the variable d
//
((Button)d.findViewById(android.R.id.button3)).setBackgroundResource(R.drawable.new_background);
编辑:如果您使用 AlertDialog.Builder 来创建它。据我所知,这些按钮分配将来可能会发生变化,因此请牢记这一点。
编辑:下面的代码块应该生成看起来像您想要的东西。事实证明你必须在更改背景之前调用 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);
关于android - 为对话框中的按钮设置背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5814998/