2011-09-25 98 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;