我遇到了一个非常非常简单的 AlertDialog
问题,它创建了我的关于对话框,其中包含三个选项。看看它在模拟器、我的 HTC Sensation 和 Galaxy S2 上的样子:
对话框由以下(伪)代码创建:
builder.setPositiveButton("OK", null);
builder.setNeutralButton("Disclaimer", [...]);
builder.setNegativeButton("Jetzt zum Market", [...]);
非常简单,我从来没有想过不同设备上的最大文本长度差异如此之大!另外:我认为三星撕毁“免责声明”真是一团糟,更不用说整个“市场”一词的缺失。
当这已经是一个问题时,我应该如何创建一个简单的 AlertDialog
?
有什么线索吗?想想一个设置这么大字体的设备,连“取消”都装不下了! 叹息我怎样才能防止这样的事情发生?
谢谢指教!!
最佳答案
Drew DeNardo 的回答几乎是正确的。
myDialog.getButton(BUTTON_POSITIVE) 返回 null,因为它尚未创建。
你需要覆盖 onShow:
final AlertDialog alert= builder.create();
alert.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
float textSize = 15.0f;
Button positive = alert.getButton(AlertDialog.BUTTON_POSITIVE);
positive.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
Button neutral = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
neutral.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
Button negative = alert.getButton(AlertDialog.BUTTON_NEGATIVE);
negative.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
}
});
关于android - 如何确定 AlertDialog 按钮的最大文本长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8113341/