je dois aligner le texte par le milieu dans android alertdialog. mais je ne peux pas trouver moyen ... quelqu'un sait comment cela?android: comment aligner un message dans alertDialog?
Répondre
Demandez à votre TextView de remplir le parent et lui donner une gravité centrale.
<TextView ... android:layout_width="fill_parent" android:gravity="center" />
Vous devez utiliser l'un des constructeurs fournis pour AlertDialog dans Android, tout en en créant un.
AlertDialog (Contexte contextuel, int theme) Construire un composant AlertDialog qui utilise un thème explicite.
Cette link vous aidera. Puisque vous voulez que le texte soit centré, vous voudriez donner l'attribut de gravité, la valeur «centre».
essayer cette
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();
Je sais que ce fil est vieux, mais pourrait aider certaines personnes: 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();
S'il vous plaît pouvez-vous écrire comment votre code est utile et diffère des autres réponses. Cela aidera les gens à lire votre réponse et à déterminer s'il faut essayer ou non (et pourrait les aider à apprendre) – Patrick
Comment ajouter le 'title' TextView dans le' builder'? –
Essayez ceci - il fera l'affaire.
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();
La meilleure façon consiste à concevoir une boîte de dialogue personnalisée.
view_dialog_box.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#A9E2F3">
<TextView
android:id="@+id/txtDiaTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Connection Alart"
android:textColor="@color/Black"
android:textStyle="bold"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#2E9AFE"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<TextView
android:id="@+id/txtDiaMsg"
android:layout_width="match_parent"
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_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="OK"
android:textColor="@color/Black"
android:textStyle="bold"
android:padding="5dp"
android:layout_margin="5dp"
android:background="@color/White"/>
Ensuite, il utilise dans le fichier 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() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Vous pouvez utiliser votre mise en page personnalisée pour la mise en page de dialogue d'alerte. Pour aligner le centre des messages de mise en page de dialogue d'alerte par défaut que vous pouvez faire
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);
Attention, si vous définissez messageText avec findViewById avant d'appeler builder.show() vous obtiendrez une exception de pointeur nul.
Il suffit d'utiliser cette méthode et votre titre de dialogue et un message apparaîtra au centre:
public static void openDialog (contexte de contexte, le message String) {
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);
}
- 1. Android TabActivity dans AlertDialog
- 2. Comment aligner des tartines android
- 3. Comment prendre en titre et icône dans AlertDialog pour Android
- 4. Analyser XML et l'afficher dans un AlertDialog dans Android
- 5. Android - AlertDialog dans onContextItemSelected ne fonctionne pas
- 6. Afficher AlertDialog depuis l'écran d'appel dans Android
- 7. Android: ne peut pas changer le texte apparaît dans AlertDialog
- 8. Android AlertDialog ne pas afficher les boutons radio ou le message
- 9. Android AlertDialog Builder Problème avec l'image
- 10. Envoyer un grand message param dans Android
- 11. Comment aligner le texte en android
- 12. ListView dans AlertDialog
- 13. Maximiser un AlertDialog?
- 14. Push message dans Android
- 15. Comment puis-je aligner le bouton radio dans Android
- 16. Comment afficher un message contextuel temporaire non modal dans Android?
- 17. Texte de saisie AlertDialog
- 18. comment appliquer la couleur dans le message texte dans la boîte de dialogue d'alerte dans Android
- 19. Où se trouve mon AlertDialog?
- 20. Comment puis-je transmettre l'ID de l'élément sélectionné d'un ListView à un AlertDialog sous Android?
- 21. Android - Modifier le texte du bouton positif d'un AlertDialog
- 22. Android: Passer une variable à l'extérieur de AlertDialog onClick
- 23. Comment poster un message sur un mur (Android + FBConnect)
- 24. Comment colorer et aligner l'élément spinner sur Android?
- 25. Application Android Email: Ouvrez un message directement
- 26. Comment aligner verticalement un TextBox dans un StackPanel?
- 27. bas aligner un lien dans un div
- 28. Message Android d'envoi
- 29. Vertical aligner un bloc
- 30. AlertDialog n'attend pas d'entrée
Comment faire la même avec AlertDialog? –