Question

Je me demandais s'il y avait un moyen d'afficher tout le texte dans un toast à centrer. Par exemple, j'ai un toast qui a 2 lignes de texte en elle. Pour des raisons purement esthétiques, je voudrais que le texte aligné au centre au lieu de aligné à gauche. Je l'ai regardé à travers la documentation et ne peut pas trouver quoi que ce soit à ce sujet. Yat-il un moyen simple de faire ce que j'ai manqué?

Merci Chris

Était-ce utile?

La solution

Utilisez la fonction setView(view) du Toast pour fournir un View avec Gravity.CENTER.

Autres conseils

Adapté de une autre réponse :

Toast toast = Toast.makeText(this, "Centered\nmessage", Toast.LENGTH_SHORT);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if( v != null) v.setGravity(Gravity.CENTER);
toast.show();

Toast est construit sur un TextView et de la gravité par défaut de celui-ci est aligné à gauche. Donc, vous devez créer votre propre TextView comme celui-ci par exemple:

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical|center_horizontal"
    android:text="all the text you want"
/>

Et vous attribuez la TextView à la Toast comme ceci:

Toast t = new Toast(yourContext);
t.setView(yourNewTextView);

Il est sale hack, mais

((TextView)((LinearLayout)toast.getView()).getChildAt(0))
    .setGravity(Gravity.CENTER_HORIZONTAL);

Sans les hacks:

String text = "Some text";
Spannable centeredText = new SpannableString(text);
centeredText.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
            0, text.length() - 1,
            Spannable.SPAN_INCLUSIVE_INCLUSIVE);

Toast.makeText(getActivity(), centeredText, Toast.LENGTH_LONG).show();

Il y a aussi un autre alignements en dehors du centre.

sources

Toast toast = Toast.makeText(this, "Message", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

Non SAING que findViewById(android.R.id.message) est faux, mais juste au cas où il y a des (futurs?) Des différences de mise en œuvre, je me suis servi d'un différen bit approche:

void centerText(View view) {
    if( view instanceof TextView){
        ((TextView) view).setGravity(Gravity.CENTER);
    }else if( view instanceof ViewGroup){
        ViewGroup group = (ViewGroup) view;
        int n = group.getChildCount();
        for( int i = 0; i<n; i++ ){
            centerText(group.getChildAt(i));
        }
    }
}

et

Toast t = Toast.makeText(context, msg,Toast.LENGTH_SHORT);
centerText(t.getView());
t.show();

Il est un travail pour moi:

Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER| Gravity.BOTTOM, 0, 20);
toast.show();

Kotlin :

fun makeToast(context: Context, resId: Int) {
    val toast = Toast.makeText(context, resId, Toast.LENGTH_SHORT)
    val view = toast.view.findViewById<TextView>(android.R.id.message)
    view?.let {
        view.gravity = Gravity.CENTER
    }
    toast.show()
}

Cette variation est à l'utilisation LinearLayout. :)

Toast SampleToast = Toast.makeText(this, "This is the example of centered text.\nIt is multiline text.", Toast.LENGTH_SHORT);
LinearLayout OurLayout = (LinearLayout) SampleToast.getView();

if (OurLayout.getChildCount() > 0) 
{
TextView SampleView = (TextView) OurLayout.getChildAt(0);
SampleView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
}

SampleToast.show();
Toast t=Toast.makeText(getApplicationContext(),"Text",Toast.LENGTH_LONG);
t.setText("Password Does't match...");
t.setGravity(0, 0, 0);
t.show();

code simple pour le pain grillé plus être le centre

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top