2012-07-11 13 views
8

Androidでダイアログをカスタマイズしたいと思います。 私は、ダイアログのタイトルを設定する方法を知っている:Androidでダイアログのアイコンを設定する方法

dialog.setTitle("O message"); 

は、今私は、タイトルの前にあるアイコンを設定したいです。 これをどうすれば実現できますか?

Dialog dialog; 
dialog = new Dialog(this); 
dialog.setContentView(R.layout.layouterror22);  
dialog.setTitle("O message"); 

答えて

23

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(); 

"Icons in custom dialogs android"を参照してください。

+3

行の順序:コンテンツがダイアログにプッシュされる前に機能を要求する必要があるため、非常に重要です。 – msysmilu

+2

行の順序:それは非常に重要です!ダイアログ.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON、R.drawable.your_icon); setContentViewの後に呼び出される必要があります –

4
dialog.setIcon(Drawable icon); 

または

dialog.setIcon(int resId); 

この情報がお役に立てば幸いです。

+0

私はdialog.setIconを取得しますが、(注:このダイヤルはカスタマイズできます)これはコードです。 \t dialog =新しいダイアログ(this); \t \t dialog.setContentView(R.layout.layouterror22); dialog.setTitle( "O message"); –

+0

QuestionはAlertDialogではなくDialogについてです。上記の答えはDialogには適用されません。上記のCarl Boschのコメントを参照してください。 – user1608385

8

使用この、

dialog.setIcon(R.drawable.ic_launcher) 

あなたはカスタマイズが意味より多くを必要とする、あなたはAlertDialogを使用する必要があるかもしれませこのサイトhttp://www.androidhive.info/2011/09/how-to-show-alert-dialog-in-android/

+2

これはダイアログですAlertDialog –

+0

@ user1497597:このリンクを参照してください。あなたはあなたのものと同じように変更することができます。http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application – Aerrow

3

を参照してください。そうであれば、ちょうど

AlertDialog.Builder b = new AlertDialog.Builder(yourContext); 
b.setIcon(yourIcon); 
/* add other properties thanks to b.set... */ 
b.create().show(); 

希望します。あなたは次のコードでアイコンを追加することができます

+0

私はdialog.setIconを取得しますが、サポートしていません(注:このdialgo customize)これはコードです:ダイアログダイアログ。 \t dialog =新しいダイアログ(this); \t \t dialog.setContentView(R.layout.layouterror22); dialog.setTitle( "O message"); –

+1

'AlertDialog'は、' setIcon() 'が動作するために' setTitle() 'が必要なようです。 – jdv

0

フラグメントを含むviewPagerを使用している場合は、MainActivityonBackPressed()から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(); 
    } 
関連する問題