2014-02-28 52 views
1

我正在嘗試調整AlertDialog的大小,我之前完成了這個操作,但不記得我是如何做到的。以下是我使用的代碼,但它只是工作。有人能夠指出我做錯了什麼。Android - 無法調整AlertDialog

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity()); 
    alertDialogBuilder 
      .setMessage(R.string.checkout_journey_selector_popup_title) 
      .setPositiveButton(R.string.checkout_journey_selector_popup_paylater_button, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          CheckoutHelper.startReservationJourney(getActivity(), 
            collectibleItems); 
         } 
        }) 
      .setNegativeButton(R.string.checkout_journey_selector_popup_paynow_button, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          CheckoutHelper.startPrepayCollectionJourney(getActivity(), 
            collectibleItems); 
         } 
        }).create().show(); 



    Dialog alert = alertDialogBuilder.create(); 
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
    lp.copyFrom(alert.getWindow().getAttributes()); 

    lp.height = 100; 
    lp.width = 50; 
    alert.getWindow().setAttributes(lp); 

在此先感謝

+0

試試這個'alertDialog.getWindow()的setLayout(600,400);''後alertDialog.show() ;' –

+0

在佈局 – Gayathiri

回答

5

此時應更換此

Dialog alert = alertDialogBuilder.create(); 
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
lp.copyFrom(alert.getWindow().getAttributes()); 
lp.height = 100; 
lp.width = 50; 
alert.getWindow().setAttributes(lp); 

隨着

WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
lp.copyFrom(alertDialogBuilder.getWindow().getAttributes()); 
lp.height = 100; 
lp.width = 50; 
alertDialogBuilder.getWindow().setAttributes(lp); 

或者試試這個

alertDialog.getWindow().setLayout(600, 400); after alertDialog.show(); 

先嚐試,並給我反饋

+0

中創建自定義警報對話框,因此我將其替換爲: Dialog alert = alertDialogBu​​ilder.create(); WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); lp.copyFrom(alert.getWindow()。getAttributes()); lp.height = 300; lp.width = 300; alert.getWindow()。setAttributes(lp); alert.show(); 但可悲的是它只是顯示在標準尺寸 –

+0

@AndrewMurphy是第一次嘗試這個 –

+0

我不能使用:(alertDialogBu​​ilder.getWindow()的getAttributes()) lp.copyFrom; 因爲getWindow不適用於AlertDialog –

0

做這樣

代替alert.getWindow().setAttributes(lp);