android : how to align message in alertDialog?

前端 未结 8 817

i have to align text by middle in android alertdialog. but i cannot find way... anyone knows how to this?

8条回答
  •  一个人的身影
    2020-12-15 04:55

    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);
    
    }
    

提交回复
热议问题