2015-06-09 11 views
16

tôi sử dụng mã này để lập trình Android (Java):nút AlertDialog align để tập trung

public static MessageBoxResult showOk(
     Context context, String title, String message, String okMessage) 
{ 
    okDialogResult = MessageBoxResult.Closed; 

    // make a handler that throws a runtime exception when a message is received 
    final Handler handler = new Handler() 
    { 
     @Override 
     public void handleMessage(Message mesg) 
     { 
      throw new RuntimeException(); 
     } 
    }; 

    AlertDialog.Builder alert = new AlertDialog.Builder(context); 
    alert.setTitle(title); 
    alert.setMessage(message); 

    alert.setPositiveButton(okMessage, new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int whichButton) { 
      okDialogResult = MessageBoxResult.Positive; 
      handler.sendMessage(handler.obtainMessage()); 
     } 
    }); 

    AlertDialog dialog = alert.show(); 


    // align button to center 
    Button b = (Button) dialog.findViewById(android.R.id.button1); 
    b.setGravity(Gravity.CENTER_HORIZONTAL); 

    // loop till a runtime exception is triggered. 
    try { Looper.loop(); } 
    catch(RuntimeException e2) {} 

    return okDialogResult; 
} 

Vấn đề của tôi là như thế nào làm cho trung tâm nút? Như bạn thấy, tôi cố gắng căn chỉnh nút để cnenter bằng cách sử dụng Gravity.CENTER_HORIZONTAL (cũng .CENTER) nhưng không có gì thay đổi. Nút gần như ở đúng vị trí.

Trả lời

9

này đã làm việc cho tôi: phương pháp

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppCompatAlertDialogStyle); 
    builder.setCancelable(true); 
    builder.setTitle(title); 
    builder.setMessage(message); 

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 

     } 
    }); 


    final AlertDialog dialog = builder.create(); 
    dialog.show(); //show() should be called before dialog.getButton(). 


    final Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE); 
    LinearLayout.LayoutParams positiveButtonLL = (LinearLayout.LayoutParams) positiveButton.getLayoutParams(); 
    positiveButtonLL.gravity = Gravity.CENTER; 
    positiveButton.setLayoutParams(positiveButtonLL); 
+0

tham khảo 'positiveBtn' là gì? – t0m

+0

Điều này thực sự có hiệu quả không? – zygimantus

+0

Không hoạt động trên Android 7.1.1 nhưng giải pháp @Scott Brown bên dưới chỉ hoạt động tốt. – mtronics

1

Tôi đoán bạn đang sử dụng AlertDialog từ thư viện Hỗ trợ.

Nếu đúng như vậy, hãy thử thay thế quá trình nhập của bạn thành android.app.AlertDialog.

22

Sử dụng crtn, nhưng thay vì thay đổi lực hấp dẫn của LayoutParam, thay đổi chiều rộng của nó để ViewGroup.LayoutParams.MATCH_PARENT;

+2

Điều này đang hoạt động, Cảm ơn –

+0

Hoạt động tốt. Để được superclear cho những người lười biếng như tôi: Thay thế dòng với

0

Sử dụng android.support.v7.app.AlertDialog sẽ căn chỉnh các nút tích cực và tiêu cực của bạn ở giữa.

android.app.AlertDialog sẽ đặt nút ở trên cùng để lại không gian 16dp ở dưới cùng.

2

Đã thử phương pháp crtn và sửa đổi của Scott Brown, cả hai đều không hiển thị cách tôi thích.

Giải pháp của crtn không thay đổi giao diện của nút cho tôi (tôi đang sử dụng android.R.style.Theme_Material_Light_Dialog) và giải pháp của Scott Brown làm nút tích cực của tôi mở rộng qua cạnh của hộp thoại cha.

Đối với Theme_Material_Light_Dialog các nút được chứa trong lớp con LinearLayout sử dụng Chế độ xem trống làm phần tử thứ 2 (chỉ mục 1) để đẩy các nút sang phải.

Tôi lấy Button ref như crtn làm:

AlertDialog dialog = bld.create(); 
dialog.show(); //show() MUST be called before dialog.getButton 
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE); 

Nhưng sau đó tôi đặt leftSpacer để View.GONE và lực hấp dẫn của cha mẹ để CENTER_HORIZONTAL

LinearLayout parent = (LinearLayout) positiveButton.getParent(); 
parent.setGravity(Gravity.CENTER_HORIZONTAL); 
View leftSpacer = parent.getChildAt(1); 
leftSpacer.setVisibility(View.GONE); 

này có ưu điểm là nó doesn 't phá vỡ hành vi xếp chồng nút của hộp thoại. Điểm bất lợi là nếu thay đổi bố cục bên trong, nó sẽ bị vỡ, vì vậy YMMV.

1

Nếu bạn muốn có Buttons cực và tiêu cực tại cùng một thời gian (Large & Center), bạn có thể sử dụng một cái gì đó như thế này:

Dialog Positive & Negative Buttons

AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
alertDialog.setTitle("Title"); 
alertDialog.setMessage("Message"); 

alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Yes", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
      } 
     }); 

alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "No", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
      } 
     }); 
alertDialog.show(); 

Button btnPositive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); 
Button btnNegative = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE); 

LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) btnPositive.getLayoutParams(); 
layoutParams.weight = 10; 
btnPositive.setLayoutParams(layoutParams); 
btnNegative.setLayoutParams(layoutParams);