如何在Android中设置对话框的图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11430253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set icon for dialog in Android
提问by D T
I want to customize a dialog in Android. I know how to set the title for dialog:
我想在 Android 中自定义一个对话框。我知道如何设置对话框的标题:
dialog.setTitle("O message");
Now I want to set the icon in front of the title. How can I achieve this?
现在我想在标题前面设置图标。我怎样才能做到这一点?
Dialog dialog;
dialog = new Dialog(this);
dialog.setContentView(R.layout.layouterror22);
dialog.setTitle("O message");
回答by D T
You can add an icon with the following code:
您可以使用以下代码添加图标:
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();
回答by Aerrow
use this,
用这个,
dialog.setIcon(R.drawable.ic_launcher)
you need more customization means, refer this site http://www.androidhive.info/2011/09/how-to-show-alert-dialog-in-android/
你需要更多的定制手段,参考这个网站http://www.androidhive.info/2011/09/how-to-show-alert-dialog-in-android/
回答by Egor
dialog.setIcon(Drawable icon);
or
或者
dialog.setIcon(int resId);
Hope this helps.
希望这可以帮助。
回答by AMerle
May be you should use an AlertDialog. If you do, just
可能您应该使用 AlertDialog。如果你这样做,只要
AlertDialog.Builder b = new AlertDialog.Builder(yourContext);
b.setIcon(yourIcon);
/* add other properties thanks to b.set... */
b.create().show();
Hope this helps you.
希望这对你有帮助。
回答by Purkey
If you are using a viewPager
with fragments, you can call a safeExit()
from the onBackPressed()
of the MainActivity
. This is what I have done, I've never had any problems:
如果您使用的是viewPager
与片段,您可以调用safeExit()
从onBackPressed()
的MainActivity
。这就是我所做的,我从来没有遇到过任何问题:
@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();
}