Android 警告对话框 - 图片在上
Android alert dialog - image on top
嘿,我想在警告对话框的标题上方放一张图片。这就是我正在做的:
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.img);
alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_DeviceDefault_Light_Dialog))
.setView(image)
.setCancelable(false)
.setTitle(title)
.setMessage(message)
.setPositiveButton(btn1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {}})
.setNegativeButton(btn2, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {}})
.create();
alertDialog.show();
这是结果:
这就是我想要实现的目标:
我也用 setIcon 尝试过,但图像出现在标题的左侧而不是上方。
您必须创建自定义布局,您可以在其中设计您想要的布局。因为您无法在默认的现有警报对话框中进行更多自定义。
您可以使用 ref url 创建的自定义警报对话框:http://javatechig.com/android/android-custom-dialog-example
只需使用此解决方案
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setTitle(R.string.my_dialog_title);
dialog.setContentView(R.layout.my_dialog_layout);
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.some_icon);
dialog.show();
您必须创建自定义对话框并为 CustomDialog 扩充该布局。
您可以在此处查看自定义对话框。
Create Custom dialog
嘿,我想在警告对话框的标题上方放一张图片。这就是我正在做的:
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.img);
alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_DeviceDefault_Light_Dialog))
.setView(image)
.setCancelable(false)
.setTitle(title)
.setMessage(message)
.setPositiveButton(btn1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {}})
.setNegativeButton(btn2, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {}})
.create();
alertDialog.show();
这是结果:
这就是我想要实现的目标:
我也用 setIcon 尝试过,但图像出现在标题的左侧而不是上方。
您必须创建自定义布局,您可以在其中设计您想要的布局。因为您无法在默认的现有警报对话框中进行更多自定义。
您可以使用 ref url 创建的自定义警报对话框:http://javatechig.com/android/android-custom-dialog-example
只需使用此解决方案
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setTitle(R.string.my_dialog_title);
dialog.setContentView(R.layout.my_dialog_layout);
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.some_icon);
dialog.show();
您必须创建自定义对话框并为 CustomDialog 扩充该布局。 您可以在此处查看自定义对话框。
Create Custom dialog