我在Android (Java)编程中使用以下代码:
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;
}
我的问题是如何使按钮居中?正如你所看到的,我尝试使用Gravity.CENTER_HORIZONTAL
(也是.CENTER
)将按钮对齐到中心,但是什么也没有改变。按钮几乎就在正确的位置。
https://stackoverflow.com/questions/30743038
复制相似问题