我想在Android中自定義對話框。 我知道如何設置標題對話框:如何在Android中設置對話框的圖標
dialog.setTitle("O message");
現在我想設置的圖標,標題前。 我該如何做到這一點?
Dialog dialog;
dialog = new Dialog(this);
dialog.setContentView(R.layout.layouterror22);
dialog.setTitle("O message");
我想在Android中自定義對話框。 我知道如何設置標題對話框:如何在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(Drawable icon);
或
dialog.setIcon(int resId);
希望這有助於。
我得到dialog.setIcon但不支持(注意:這dialgo自定義)這可能是代碼:對話框對話框; \t dialog = new Dialog(this); \t \t dialog.setContentView(R.layout.layouterror22); dialog.setTitle(「O消息」); – 2012-07-12 02:19:26
問題是關於Dialog,而不是AlertDialog。上面的答案不適用於Dialog。請參閱上面的Carl Bosch評論。 – user1608385 2016-11-29 23:06:01
使用此,
dialog.setIcon(R.drawable.ic_launcher)
你需要更多的定製方式,請參閱本網站http://www.androidhive.info/2011/09/how-to-show-alert-dialog-in-android/
這是對話框不AlertDialog – 2012-07-12 02:21:14
@ user1497597:引用此鏈接它有一個例子,你可以改變爲你的http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application – Aerrow 2012-07-12 08:23:36
可能是你應該使用AlertDialog。如果你這樣做,只是
AlertDialog.Builder b = new AlertDialog.Builder(yourContext);
b.setIcon(yourIcon);
/* add other properties thanks to b.set... */
b.create().show();
希望這可以幫助你。
我得到dialog.setIcon但不支持(注意:這個dialgo自定義)這可能是代碼:對話框對話框; \t dialog = new Dialog(this); \t \t dialog.setContentView(R.layout.layouterror22); dialog.setTitle(「O消息」); – 2012-07-12 02:18:07
'AlertDialog'似乎需要'setTitle()'應用於'setIcon()'才能工作。 – jdv 2016-06-03 15:38:17
如果您使用帶有片段的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();
}
線條的順序:這是非常重要的,因爲在將任何內容推送到對話框之前必須請求功能。 – msysmilu 2015-02-03 21:03:54
線條的順序:非常重要! dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.your_icon);應該在setContentView之後調用 – 2015-11-17 22:04:42