2011-09-25 148 views
1

如何在不使用xml的情況下更改代碼中alertdailog中按鈕的大小?你爲什麼要 alertbox3.setNeutralButton("Cancel",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } });alertdialog中按鈕的大小

回答

2

,你可以嘗試這樣的代碼:

AlertDialog.Builder adb = new AlertDialog.Builder(this); 
Dialog d = adb.setView(new View(this)).create(); 
// (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.) 

WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
lp.copyFrom(d.getWindow().getAttributes()); 
lp.width = WindowManager.LayoutParams.FILL_PARENT; 
lp.height = WindowManager.LayoutParams.FILL_PARENT; 
d.show(); 
d.getWindow().setAttributes(lp); 

http://developer.android.com/reference/android/widget/Button.html

看看這個鏈接ASLO。 Link1LInk2

-1

: 我不使用視圖..

謝謝 的代碼?系統將以您的名義構建標準對話框佈局,遵循用戶期望和熟悉的約定。總的來說,你不應該爲了規避這個問題而走出困境。

+0

但是如果我想要把長的語句按鈕像(閉上你的應用程序) – Dev

+0

對話框按鈕上的文字應短,使用對話框的其餘部分給選項的上下文。再次,這歸結爲應用程序應遵循的平臺慣例。 – adamp

+0

這只是部分正確。我遇到了類似的問題,因爲即使是「免責聲明」等「簡短」文本也不適合於某些設備!對於Android設備製造商來說,改變字體大小並使您的按鈕包裝文字真的是一個恥辱...... – Zordid

0

試試這個: https://stackoverflow.com/a/15910202/305135

final AlertDialog alert = builder.create(); 
alert.setOnShowListener(new DialogInterface.OnShowListener() { 
    @Override 
    public void onShow(DialogInterface dialog) { 
     Button btnPositive = alert.getButton(Dialog.BUTTON_POSITIVE); 
     btnPositive.setTextSize(TEXT_SIZE); 

     Button btnNegative = alert.getButton(Dialog.BUTTON_NEGATIVE); 
     btnNegative.setTextSize(TEXT_SIZE); 
    } 
}); 

return alert;