问题
I am trying to display an lengthy message in Default AlertDialog with following code.
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();
}
But when I pass long message Alert Dialog does not display full message. Can I align text of Dialog by center. I dont want to use Custom Dialog.
回答1:
Change
dialog.setTitle(message);
to
dialog.setMessage(message);
and if you want to center align the text, you'll have to create a textview like in the following example:
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);
taken from: Center message in android dialog box
回答2:
try following code
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();
}
回答3:
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);
来源:https://stackoverflow.com/questions/10699644/show-long-message-in-alertdialog