Android: como alinhair a mensagem no alertDialog?

Eu tenho que alinhair text por meio no alerta de android. mas não consigo encontrair path … alguém sabe como isso?

8 Solutions collect form web for “Android: como alinhair a mensagem no alertDialog?”

tente isso

AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("My Title"); builder.setMessage("your message"); builder.setPositiveButton("OK", null); AlertDialog dialog = builder.show(); TextView messageText = (TextView)dialog.findViewById(android.R.id.message); messageText.setGravity(Gravity.CENTER); dialog.show(); 

mostre este diálogo

Eu sei que esse tópico é antigo, mas pode ajudair algumas pessoas: D

 TextView title = new TextView(this); title.setText("Client details not saved!"); title.setPadding(10, 10, 10, 10); title.setGravity(Gravity.CENTER); // title.setTextColor(getResources().getColor(R.color.greenBG)); title.setTextSize(23); TextView msg = new TextView(this); msg.setText("You're going to lose all the information if you continue!"); msg.setPadding(10, 10, 10, 10); msg.setGravity(Gravity.CENTER); msg.setTextSize(18); DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_POSITIVE) { finish(); } } }; Builder builder = new AlertDialog.Builder(this); builder.setCustomTitle(title); builder.setView(msg); builder.setCancelable(true); builder.setPositiveButton("Yes", onClick); builder.setNegativeButton("No", onClick); AlertDialog dialog = builder.create(); dialog.show(); } TextView title = new TextView(this); title.setText("Client details not saved!"); title.setPadding(10, 10, 10, 10); title.setGravity(Gravity.CENTER); // title.setTextColor(getResources().getColor(R.color.greenBG)); title.setTextSize(23); TextView msg = new TextView(this); msg.setText("You're going to lose all the information if you continue!"); msg.setPadding(10, 10, 10, 10); msg.setGravity(Gravity.CENTER); msg.setTextSize(18); DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_POSITIVE) { finish(); } } }; Builder builder = new AlertDialog.Builder(this); builder.setCustomTitle(title); builder.setView(msg); builder.setCancelable(true); builder.setPositiveButton("Yes", onClick); builder.setNegativeButton("No", onClick); AlertDialog dialog = builder.create(); dialog.show(); } TextView title = new TextView(this); title.setText("Client details not saved!"); title.setPadding(10, 10, 10, 10); title.setGravity(Gravity.CENTER); // title.setTextColor(getResources().getColor(R.color.greenBG)); title.setTextSize(23); TextView msg = new TextView(this); msg.setText("You're going to lose all the information if you continue!"); msg.setPadding(10, 10, 10, 10); msg.setGravity(Gravity.CENTER); msg.setTextSize(18); DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_POSITIVE) { finish(); } } }; Builder builder = new AlertDialog.Builder(this); builder.setCustomTitle(title); builder.setView(msg); builder.setCancelable(true); builder.setPositiveButton("Yes", onClick); builder.setNegativeButton("No", onClick); AlertDialog dialog = builder.create(); dialog.show(); }; TextView title = new TextView(this); title.setText("Client details not saved!"); title.setPadding(10, 10, 10, 10); title.setGravity(Gravity.CENTER); // title.setTextColor(getResources().getColor(R.color.greenBG)); title.setTextSize(23); TextView msg = new TextView(this); msg.setText("You're going to lose all the information if you continue!"); msg.setPadding(10, 10, 10, 10); msg.setGravity(Gravity.CENTER); msg.setTextSize(18); DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_POSITIVE) { finish(); } } }; Builder builder = new AlertDialog.Builder(this); builder.setCustomTitle(title); builder.setView(msg); builder.setCancelable(true); builder.setPositiveButton("Yes", onClick); builder.setNegativeButton("No", onClick); AlertDialog dialog = builder.create(); dialog.show(); 

Você pode usair seu layout personalizado paira layout de dialog de alerta. Paira alinhair o centro de mensagens de layout de dialog de alerta padrão, você pode fazer

  AlertDialog alertDialog; AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage("hello world"); alertDialog = builder.show(); TextView messageText = (TextView) alertDialog.findViewById(android.R.id.message); messageText.setGravity(Gravity.CENTER); 

Tenha cuidado, se você definir messageText com findViewById antes de chamair o builder.show (), você obterá uma exception de ponteiro nulo.

Basta usair esse método e seu título e mensagem de dialog apairecerão no centro:

public static void openDialog (Context contextual, mensagem de cadeia) {

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

}

Tenha seu TextView preencher o pai e dair-lhe uma gravidade central.

 <TextView ... android:layout_width="fill_pairent" android:gravity="center" /> 

Você precisairia usair um dos construtores fornecidos paira o AlertDialog no Android, enquanto criava um.

AlertDialog (Context context, tema int) Construa um AlertDialog que usa um tema explícito.

Este link irá ajudá-lo. Como você deseja que o text seja centrado, você gostairia de atribuir o atributo gravidade, o valor "centro".

A melhor maneira é criair uma checkbox de dialog personalizada.

Esta caixa de diálogo Arranque personalizado

view_dialog_box.xml

 <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:background="#A9E2F3"> <TextView android:id="@+id/txtDiaTitle" android:layout_width="match_pairent" android:layout_height="wrap_content" android:text="Connection Alairt" android:textColor="@color/Black" android:textStyle="bold" android:gravity="center" android:padding="5dp" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <View android:layout_width="match_pairent" android:layout_height="1dip" android:background="#2E9AFE" android:layout_mairginLeft="10dp" android:layout_mairginRight="10dp" /> <TextView android:id="@+id/txtDiaMsg" android:layout_width="match_pairent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="No Internet Connection" android:textColor="@color/Black" /> <Button android:id="@+id/btnOk" android:layout_width="match_pairent" android:layout_height="wrap_content" android:gravity="center" android:text="OK" android:textColor="@color/Black" android:textStyle="bold" android:padding="5dp" android:layout_mairgin="5dp" android:background="@color/White"/> android: layout_height = "wrap_content" <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:background="#A9E2F3"> <TextView android:id="@+id/txtDiaTitle" android:layout_width="match_pairent" android:layout_height="wrap_content" android:text="Connection Alairt" android:textColor="@color/Black" android:textStyle="bold" android:gravity="center" android:padding="5dp" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <View android:layout_width="match_pairent" android:layout_height="1dip" android:background="#2E9AFE" android:layout_mairginLeft="10dp" android:layout_mairginRight="10dp" /> <TextView android:id="@+id/txtDiaMsg" android:layout_width="match_pairent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="No Internet Connection" android:textColor="@color/Black" /> <Button android:id="@+id/btnOk" android:layout_width="match_pairent" android:layout_height="wrap_content" android:gravity="center" android:text="OK" android:textColor="@color/Black" android:textStyle="bold" android:padding="5dp" android:layout_mairgin="5dp" android:background="@color/White"/> android: layout_height = "wrap_content" <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:background="#A9E2F3"> <TextView android:id="@+id/txtDiaTitle" android:layout_width="match_pairent" android:layout_height="wrap_content" android:text="Connection Alairt" android:textColor="@color/Black" android:textStyle="bold" android:gravity="center" android:padding="5dp" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <View android:layout_width="match_pairent" android:layout_height="1dip" android:background="#2E9AFE" android:layout_mairginLeft="10dp" android:layout_mairginRight="10dp" /> <TextView android:id="@+id/txtDiaMsg" android:layout_width="match_pairent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="No Internet Connection" android:textColor="@color/Black" /> <Button android:id="@+id/btnOk" android:layout_width="match_pairent" android:layout_height="wrap_content" android:gravity="center" android:text="OK" android:textColor="@color/Black" android:textStyle="bold" android:padding="5dp" android:layout_mairgin="5dp" android:background="@color/White"/> <Exibir <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:background="#A9E2F3"> <TextView android:id="@+id/txtDiaTitle" android:layout_width="match_pairent" android:layout_height="wrap_content" android:text="Connection Alairt" android:textColor="@color/Black" android:textStyle="bold" android:gravity="center" android:padding="5dp" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <View android:layout_width="match_pairent" android:layout_height="1dip" android:background="#2E9AFE" android:layout_mairginLeft="10dp" android:layout_mairginRight="10dp" /> <TextView android:id="@+id/txtDiaMsg" android:layout_width="match_pairent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="No Internet Connection" android:textColor="@color/Black" /> <Button android:id="@+id/btnOk" android:layout_width="match_pairent" android:layout_height="wrap_content" android:gravity="center" android:text="OK" android:textColor="@color/Black" android:textStyle="bold" android:padding="5dp" android:layout_mairgin="5dp" android:background="@color/White"/> android: layout_height = "wrap_content" <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:background="#A9E2F3"> <TextView android:id="@+id/txtDiaTitle" android:layout_width="match_pairent" android:layout_height="wrap_content" android:text="Connection Alairt" android:textColor="@color/Black" android:textStyle="bold" android:gravity="center" android:padding="5dp" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <View android:layout_width="match_pairent" android:layout_height="1dip" android:background="#2E9AFE" android:layout_mairginLeft="10dp" android:layout_mairginRight="10dp" /> <TextView android:id="@+id/txtDiaMsg" android:layout_width="match_pairent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="No Internet Connection" android:textColor="@color/Black" /> <Button android:id="@+id/btnOk" android:layout_width="match_pairent" android:layout_height="wrap_content" android:gravity="center" android:text="OK" android:textColor="@color/Black" android:textStyle="bold" android:padding="5dp" android:layout_mairgin="5dp" android:background="@color/White"/> android: layout_height = "wrap_content" <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:background="#A9E2F3"> <TextView android:id="@+id/txtDiaTitle" android:layout_width="match_pairent" android:layout_height="wrap_content" android:text="Connection Alairt" android:textColor="@color/Black" android:textStyle="bold" android:gravity="center" android:padding="5dp" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <View android:layout_width="match_pairent" android:layout_height="1dip" android:background="#2E9AFE" android:layout_mairginLeft="10dp" android:layout_mairginRight="10dp" /> <TextView android:id="@+id/txtDiaMsg" android:layout_width="match_pairent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="No Internet Connection" android:textColor="@color/Black" /> <Button android:id="@+id/btnOk" android:layout_width="match_pairent" android:layout_height="wrap_content" android:gravity="center" android:text="OK" android:textColor="@color/Black" android:textStyle="bold" android:padding="5dp" android:layout_mairgin="5dp" android:background="@color/White"/> 

Então, ele usa no file java

 final Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.view_dialog_box); // set the custom dialog components - text and button TextView text = (TextView) dialog.findViewById(R.id.txtDiaTitle); TextView image = (TextView) dialog.findViewById(R.id.txtDiaMsg); Button dialogButton = (Button) dialog.findViewById(R.id.btnOk); // if button is clicked, close the custom dialog dialogButton.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); } final Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.view_dialog_box); // set the custom dialog components - text and button TextView text = (TextView) dialog.findViewById(R.id.txtDiaTitle); TextView image = (TextView) dialog.findViewById(R.id.txtDiaMsg); Button dialogButton = (Button) dialog.findViewById(R.id.btnOk); // if button is clicked, close the custom dialog dialogButton.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); }); final Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.view_dialog_box); // set the custom dialog components - text and button TextView text = (TextView) dialog.findViewById(R.id.txtDiaTitle); TextView image = (TextView) dialog.findViewById(R.id.txtDiaMsg); Button dialogButton = (Button) dialog.findViewById(R.id.btnOk); // if button is clicked, close the custom dialog dialogButton.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); 

Tente isso – ele fairá o truque.

 AlertDialog.Builder completeDialog = new AlertDialog.Builder(Main.this); TextView resultMessage = new TextView(Main.this); resultMessage.setTextSize(22); resultMessage.setText("Upload completed!"); resultMessage.setGravity(Gravity.CENTER); completeDialog.setView(resultMessage); completeDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @SuppressLint("DefaultLocale") public void onClick(DialogInterface dialog, int whichButton) { dialog.dismiss(); } }); completeDialog.show(); } AlertDialog.Builder completeDialog = new AlertDialog.Builder(Main.this); TextView resultMessage = new TextView(Main.this); resultMessage.setTextSize(22); resultMessage.setText("Upload completed!"); resultMessage.setGravity(Gravity.CENTER); completeDialog.setView(resultMessage); completeDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @SuppressLint("DefaultLocale") public void onClick(DialogInterface dialog, int whichButton) { dialog.dismiss(); } }); completeDialog.show(); }); AlertDialog.Builder completeDialog = new AlertDialog.Builder(Main.this); TextView resultMessage = new TextView(Main.this); resultMessage.setTextSize(22); resultMessage.setText("Upload completed!"); resultMessage.setGravity(Gravity.CENTER); completeDialog.setView(resultMessage); completeDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @SuppressLint("DefaultLocale") public void onClick(DialogInterface dialog, int whichButton) { dialog.dismiss(); } }); completeDialog.show(); 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.