Center message in android dialog box

前端 未结 8 831
野趣味
野趣味 2020-11-29 01:53

I want the message text within my dialog box to be center aligned.

8条回答
  •  执念已碎
    2020-11-29 02:07

    Just use that method and your dialog title and message will appear at center:

    public static void openDialog(Context context, String message) {

        TextView title = new TextView(context);
        // You Can Customise your Title here
        title.setText("Information Message");
        title.setBackgroundColor(Color.BLACK);
        title.setPadding(10, 15, 15, 10);
        title.setGravity(Gravity.CENTER);
        title.setTextColor(Color.WHITE);
        title.setTextSize(22);
    
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setCustomTitle(title);
        alertDialog.setMessage(message);
    
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
    
            }
        });
        alertDialog.show();
    
        // You Can Customise your Message here
        TextView messageView = (TextView) alertDialog
                .findViewById(android.R.id.message);
        messageView.setGravity(Gravity.CENTER);
    
    }
    

提交回复
热议问题