我想要自定义Android中的对话框。 我知道如何为对话框设置标题:
dialog.setTitle("O message");
现在我想把图标放在标题前面。我该怎么做呢?
Dialog dialog;
dialog = new Dialog(this);
dialog.setContentView(R.layout.layouterror22);
dialog.setTitle("O message");
我想要自定义Android中的对话框。 我知道如何为对话框设置标题:
dialog.setTitle("O message");
现在我想把图标放在标题前面。我该怎么做呢?
Dialog dialog;
dialog = new Dialog(this);
dialog.setContentView(R.layout.layouterror22);
dialog.setTitle("O message");
您可以使用以下代码添加图标:
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.your_icon);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Dialog Title");
dialog.show();
dialog.setIcon(R.drawable.ic_launcher)
如果你需要更多的自定义选项,可以参考这个网站http://www.androidhive.info/2011/09/how-to-show-alert-dialog-in-android/
dialog.setIcon(Drawable icon);
或者
dialog.setIcon(int resId);
AlertDialog.Builder b = new AlertDialog.Builder(yourContext);
b.setIcon(yourIcon);
/* add other properties thanks to b.set... */
b.create().show();
AlertDialog
应用 setTitle()
才能使 setIcon()
生效。 - user1531971AlertDialog.Builder builder = new AlertDialog.Builder(this)
builder.setTitle("Alert");
***builder.setIcon(R.drawable.ic_baseline_call_24)***
builder.setPositiveButton("Visit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
/* builder.setMessage("Visit Be Developers.tech");
builder.setTitle("Alert");
builder.setIcon(R.drawable.ic_baseline_call_24);//This line should be added
builder.setPositiveButton("Visit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
如需进行更多自定义,例如添加列表、选项等,请查看https://bedevelopers.tech/alert-dialog-in-android-show-alert-using-the-builder/。
viewPager
,则可以在 MainActivity
的 onBackPressed()
中调用 safeExit()
。 这就是我所做的,我从未遇到任何问题。 @Override
public void onBackPressed() {
try {
if (getFragmentManager().getBackStackEntryCount() > 1) {
FragmentManager.BackStackEntry first = getFragmentManager().getBackStackEntryAt(0);
getFragmentManager().popBackStack(first.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
} else
safeExit();
} catch (Exception e) {
e.printStackTrace();
}
}
private void safeExit() {
new Builder(this).setIcon(R.drawable.ic_launcher).setTitle("Exit!").setMessage(
"Close Application?").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish();
}
}).setNegativeButton("No", null).show();
}