在android对话框中心消息

我希望我的对话框中的消息文本中心alignment。

创build自己的TextView对象,然后将其作为View提供给popup窗体生成器:

AlertDialog.Builder popupBuilder = new AlertDialog.Builder(this); TextView myMsg = new TextView(this); myMsg.setText("Central"); myMsg.setGravity(Gravity.CENTER_HORIZONTAL); popupBuilder.setView(myMsg); 

您可以控制所有其他文本参数(样式,颜色,大小…)。 要控制页边距,您可以编程方式创buildLinearLayout,设置LayoutParams,然后将TextView放入其中。

当然,你可以随时设置原始文本视图的重力。 这使您不必担心格式和填充。

例如

 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Title"); builder.setMessage("Message"); builder.setPositiveButton("OK", null); AlertDialog dialog = builder.show(); // Must call show() prior to fetching text view TextView messageView = (TextView)dialog.findViewById(android.R.id.message); messageView.setGravity(Gravity.CENTER); 

从Chase的回答开始,下面介绍如何使标题居中。 我觉得这是最简单的方法。 为什么android没有默认居中,或者使它成为一个简单的构造函数参数超出了我。

 AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("My Title"); builder.setMessage("My message"); builder.setPositiveButton("OK", listener); AlertDialog dialog = builder.show(); // Must call show() prior to fetching views TextView messageView = (TextView)dialog.findViewById(android.R.id.message); messageView.setGravity(Gravity.CENTER); TextView titleView = (TextView)dialog.findViewById(context.getResources().getIdentifier("alertTitle", "id", "android")); if (titleView != null) { titleView.setGravity(Gravity.CENTER); } 

我们可以使用像。

 public static void showAlert(Activity activity, String message) { TextView title = new TextView(activity); // You Can Customise your Title here title.setText("Your Title Here"); //title.setBackgroundColor(Color.DKGRAY); title.setPadding(10, 10, 10, 10); title.setGravity(Gravity.CENTER); title.setTextColor(Color.WHITE); title.setTextSize(20); AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setCustomTitle(title); //builder.setIcon(R.drawable.alert_36); builder.setMessage(message); builder.setCancelable(false); builder.setNegativeButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.show(); TextView messageText = (TextView)alert.findViewById(android.R.id.message); messageText.setGravity(Gravity.CENTER); messageText.setTextColor(Color.RED); } 

只要使用该方法,您的对话标题和消息将出现在中心:

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

在你的TextView中试试这个:

 android:gravity = "center_vertical|center" 

你可以试试这个代码

 public void Info(){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Info Aplication"); builder.setIcon(R.drawable.info_adp); builder.setMessage("Jakarta Hospital"); builder.setCancelable(false); builder.setPositiveButton("Exit", null); AlertDialog dialog = builder.show(); TextView messageView = (TextView)dialog.findViewByI(android.R.id.message); messageView.setGravity(Gravity.CENTER); }