2012-07-11 32 views
8

Tôi muốn tùy chỉnh hộp thoại trong Android. Tôi biết cách đặt tiêu đề cho hộp thoại:Cách đặt biểu tượng cho hộp thoại trong Android

dialog.setTitle("O message"); 

Bây giờ tôi muốn đặt biểu tượng ở phía trước tiêu đề. Làm thế nào tôi có thể đạt được điều này?

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

Trả lời

23

Bạn có thể thêm một biểu tượng với đoạn mã sau:

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

Xem "Icons in custom dialogs android".

+3

Thứ tự của các dòng: điều rất quan trọng vì các tính năng phải được yêu cầu trước khi bất kỳ nội dung nào được đẩy vào hộp thoại. – msysmilu

+2

Thứ tự của các dòng: nó rất quan trọng !!! dialog.setFeatureDrawableResource (Window.FEATURE_LEFT_ICON, R.drawable.your_icon); nên được gọi sau setContentView –

4
dialog.setIcon(Drawable icon); 

hoặc

dialog.setIcon(int resId); 

Hope this helps.

+0

tôi nhận được dialog.setIcon nhưng không hỗ trợ (Lưu ý: dialgo tùy chỉnh này) này là mã tháng: hộp thoại Dialog; \t dialog = new Dialog (this); \t \t dialog.setContentView (R.layout.layouterror22); dialog.setTitle ("O message"); –

+0

Câu hỏi là về Dialog, không phải AlertDialog. Câu trả lời ở trên không áp dụng cho Hộp thoại. Xem Carl Bosch bình luận ở trên. – user1608385

8

sử dụng này,

dialog.setIcon(R.drawable.ic_launcher) 

bạn cần nhiều tùy biến nghĩa, tham khảo trang web này http://www.androidhive.info/2011/09/how-to-show-alert-dialog-in-android/

+2

đây là Dialog không AlertDialog –

+0

@ user1497597: tham khảo liên kết này nó có một ví dụ bạn có thể thực hiện thay đổi như của bạn http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application – Aerrow

3

Có thể bạn nên sử dụng một AlertDialog. Nếu bạn làm vậy, chỉ cần

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

Hy vọng điều này sẽ giúp bạn.

+0

Tôi nhận được dialog.setIcon nhưng không hỗ trợ (Lưu ý: tùy chỉnh quay số này) đây có thể là mã: Hộp thoại Dialog; \t dialog = new Dialog (this); \t \t dialog.setContentView (R.layout.layouterror22); dialog.setTitle ("O message"); –

+1

'AlertDialog' dường như cần' setTitle() 'áp dụng cho' setIcon() 'để làm việc. – jdv

0

Nếu bạn đang sử dụng viewPager với các đoạn, bạn có thể gọi số safeExit() từ số onBackPressed() của số MainActivity. Đây là những gì tôi đã làm, tôi chưa bao giờ có bất kỳ vấn đề:

@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(); 
    } 
Các vấn đề liên quan