3

我正在尝试使用以下代码在 Default AlertDialog 中显示一条冗长的消息。

public static void showAlert(String message, Context con) {
    AlertDialog.Builder dialog = new AlertDialog.Builder(con);
    dialog.setTitle(message);
    dialog.setPositiveButton(" OK ", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();

        }
    });
    dialog.show();

}

但是当我传递长消息时,警报对话框不显示完整消息。我可以按中心对齐对话框的文本吗?我不想使用自定义对话框。

4

3 回答 3

13

改变

dialog.setTitle(message);

dialog.setMessage(message);

如果要居中对齐文本,则必须创建一个文本视图,如下例所示:

    AlertDialog.Builder dialog = new AlertDialog.Builder(con);
    dialog.setTitle(message);
    dialog.setPositiveButton(" OK ", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();

        }
    });
    dialog.show();

// Must call show() prior to fetching text view
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);

取自:Android对话框中的中心消息

于 2012-05-22T10:02:30.793 回答
2

尝试以下代码

public static void showAlert(String message, Context con) {
    AlertDialog.Builder dialog = new AlertDialog.Builder(con);
    dialog.setMessage(message);
    dialog.setPositiveButton(" OK ", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();

        }
    });
    dialog.show();

}
于 2012-05-22T10:02:32.353 回答
1
AlertDialog.Builder builder1 = new AlertDialog.Builder(getParent());

builder1.setTitle("Login Alert");

TextView myMsg = new TextView(getParent());

myMsg.setText("You have not logged in to save the product.Do you want to login?");

myMsg.setTextSize(20);

myMsg.setGravity(Gravity.CENTER_HORIZONTAL);

builder1.setView(myMsg);
于 2013-07-30T05:32:28.463 回答