2010-09-09 36 views

Trả lời

13

Kể từ onPrepareDialog bị phản đối, bạn có thể sử dụng onShowListener để thay thế.

Ngoài ra, bạn nên đặt các giới hạn có thể vẽ hoặc nó sẽ được đặt ở bên trái.

Output của Bộ luật dưới đây

Output of Code below

public class MyDialog extends DialogFragment { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     final AlertDialog dialog = new AlertDialog.Builder(getActivity()) 
       .setTitle("My Dialog") 
       .setNegativeButton("Cancel", new OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 
        } 
       }).setPositiveButton("Play", new OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 
        } 
       }).create(); 
     dialog.setOnShowListener(new OnShowListener() { 

      @Override 
      public void onShow(DialogInterface dialogInterface) { 
       Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE); 

       // if you do the following it will be left aligned, doesn't look 
       // correct 
       // button.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_media_play, 
       // 0, 0, 0); 

       Drawable drawable = getActivity().getResources().getDrawable(
         android.R.drawable.ic_media_play); 

       // set the bounds to place the drawable a bit right 
       drawable.setBounds((int) (drawable.getIntrinsicWidth() * 0.5), 
         0, (int) (drawable.getIntrinsicWidth() * 1.5), 
         drawable.getIntrinsicHeight()); 
       button.setCompoundDrawables(drawable, null, null, null); 

       // could modify the placement more here if desired 
       // button.setCompoundDrawablePadding(); 
      } 
     }); 
     return dialog; 
    } 
} 
+0

Cảm ơn bạn đã cập nhật :) –

+0

Xin chào Tôi muốn hiển thị biểu tượng gần hơn để phát văn bản. vậy bạn có thể gợi ý cho tôi không? –

+0

Bạn có thể cho ví dụ về cách sử dụng mã của bạn không? Làm thế nào để khởi tạo hộp thoại? –

7

Sau khi bạn đã xây dựng được các AlertDialog trong onCreateDialog bạn có thể sử dụng đoạn mã sau vào onPrepareDialog để thêm hình ảnh vào nút tích cực:

@Override 
protected void onPrepareDialog(int id, Dialog dialog) { 
    super.onPrepareDialog(id, dialog); 
    AlertDialog alertDialog = (AlertDialog)dialog; 
    Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); 
    button.setCompoundDrawablesWithIntrinsicBounds(this.getResources().getDrawable(
      R.drawable.icon), null, null, null); 

} 

Đang cố gắng để thêm drawable nút trong phương pháp onCreateDialog không dường như hoạt động.

+0

Sẽ cố gắng và quay lại với Frank này. –

4

Bạn không thể thêm các nút trong onCreateDialog và PHẢI làm điều đó trong onPrepareDialog vì AlertDialog được xử lý theo một cách rất đặc biệt bởi android:

bạn thực sự không thực sự giữ một tham chiếu đến hộp thoại thực sự khi bạn sử dụng hộp thoại cảnh báo, đối tượng bạn nhận được bằng cách sử dụng AlertDialog.Builder.create() chỉ là một khuôn mặt cho một bộ điều khiển bên trong.

Và trước khi tạo thực sự được gọi, không có bộ điều khiển như vậy trong jvm. Chỉ là mặt tiền. Vì vậy, cho đến khi phương thức này được gọi (vào cuối onCreateDialog nếu bạn cho phép hoạt động của bạn quản lý các hộp thoại riêng của nó), thì bộ điều khiển thực sự không tồn tại và các nút thực không có.

thương hiệu SOF mới commenter, Stéphane

5

Điều này có thể được thực hiện bằng cách nhận một tham chiếu đến các nút sử dụng) phương pháp getButton (:

alert.show(); 
Button email = alert.getButton(AlertDialog.BUTTON_NEUTRAL); 
email.setBackgroundResource(R.drawable.email); 

Lưu ý rằng bạn phải sử dụng getButton() sau khi gọi phương thức show(), nếu không bạn sẽ nhận được một NullPointerException ..

+0

Sẽ kiểm tra và liên hệ lại với bạn. Cảm ơn. –

0

1. đầu tiên tạo một tệp sơ đồ bố trí mới để lưu trữ các imagebuttons: new_layout.xml;

<?xml version="1.0" encoding="UTF-8" ?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_margin="15dp" 
    android:gravity="center_horizontal" 
    android:background = "#FFFFFF" 
    android:orientation="horizontal"> 

    <!-- game button --> 
    <ImageButton 
     android:id="@+id/game" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_margin="5dp" 
     android:layout_gravity="bottom" 
     android:background = "#00ffffff" 
     android:src="@drawable/game"/> 

    <!-- browser button --> 
    <ImageButton 
     android:id="@+id/browser" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_margin="5dp" 
     android:layout_gravity="bottom" 
     android:background = "#00ffffff" 
     android:src="@drawable/browser"/> 

    <!-- email button --> 
    <ImageButton 
     android:id="@+id/email" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_margin="5dp" 
     android:layout_gravity="bottom" 
     android:background = "#00ffffff" 
     android:src="@drawable/email"/> 

</LinearLayout> 

2.Add mã dưới đây để nơi bạn muốn đối thoại để hiển thị:

final AlertDialog alertDialog = new AlertDialog.Builder(TalkerActivity.this).create(); 
    alertDialog.show(); 
    Window win = alertDialog.getWindow(); 
    win.setContentView(R.layout.new_layout); 

    //Game 
    ImageButton game_btn = (ImageButton)win.findViewById(R.id.game); 
    game_btn.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 

    //Browser 
    ImageButton browser_btn = (ImageButton)win.findViewById(R.id.browser); 
    browser_btn.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 

    //Email 
    ImageButton email_btn = (ImageButton)win.findViewById(R.id.email); 
    email_btn.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 

liên kết: http://blog.csdn.net/willproud/article/details/9191971

2

Như @aaronvargas nói, sử dụng onShowListener. Tôi sẽ cải thiện câu trả lời của mình một chút, vì đối với các thiết bị cũ hơn/nhỏ hơn, hình ảnh chồng chéo lên văn bản.Đây là mã onShow:

@Override 
public void onShow(DialogInterface dialogInterface) { 
    Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE); 
    button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.your_img, 0, 0, 0); 
    Utils.centerImageAndTextInButton(button); 
} 

Dưới đây là một chức năng tiện ích để tập trung một hình ảnh trái và văn bản bên trong một Button:

public static void centerImageAndTextInButton(Button button) { 
    Rect textBounds = new Rect(); 
    //Get text bounds 
    CharSequence text = button.getText(); 
    if (text != null && text.length() > 0) { 
     TextPaint textPaint = button.getPaint(); 
     textPaint.getTextBounds(text.toString(), 0, text.length(), textBounds); 
    } 
    //Set left drawable bounds 
    Drawable leftDrawable = button.getCompoundDrawables()[0]; 
    if (leftDrawable != null) { 
     Rect leftBounds = leftDrawable.copyBounds(); 
     int width = button.getWidth() - (button.getPaddingLeft() + button.getPaddingRight()); 
     int leftOffset = (width - (textBounds.width() + leftBounds.width()) - button.getCompoundDrawablePadding())/2 - button.getCompoundDrawablePadding(); 
     leftBounds.offset(leftOffset, 0); 
     leftDrawable.setBounds(leftBounds); 
    } 
} 

chức năng cuối cùng này sử dụng độ rộng của Button để làm cho việc tính toán, vì vậy bạn phải kiểm tra xem bạn có đang gọi điều này ở đúng nơi không. Tức là, chiều rộng phải khác 0. Trong trường hợp này, hãy gọi điện thoại từ số onShow đúng địa điểm :).

Các vấn đề liên quan