2016-12-07 1 views
0

J'ai du mal à créer mon application dans Android studio, je me expliquemodifier Dynamiquement mise en page d'un toast Android

J'ai créé une mise en page pour mon pain grillé, XML ressemble à ceci

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/elemento_correcto_s" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:background="#afff45" 
android:weightSum="1"> 
<ImageView android:layout_height="100dp" android:layout_width="100dp" android:src="@drawable/base" android:padding="5dip" android:id="@+id/ok" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="false"> 
</ImageView> 
<TextView android:layout_height="50dp" android:id="@+id/tv" android:layout_width="match_parent" android:text="¡CORRECTO!" android:textColor="#FFF" android:gravity="center_vertical|center_horizontal" 
    android:layout_gravity="center_vertical" 
    android:layout_toRightOf="@+id/ok" 
    android:layout_marginTop="26dp" 
    > 
</TextView> 

Il fonctionne très bien quand je gonfler le mise en page

LayoutInflater inflater = getLayoutInflater(); 
    View layout = inflater.inflate(
      R.layout.elemento_correcto_s 
      , (ViewGroup) findViewById(R.id.elemento_correcto_s)); 
    this.elementoCorrecto = new Toast(this); 
    this.elementoCorrecto.setGravity(Gravity.TOP, 0, 0); 
    this.elementoCorrecto.setDuration(Toast.LENGTH_LONG); 
    this.elementoCorrecto.setView(layout); 
    this.elementoCorrecto.show(); 

Mais le problème est que je souhaite modifier dynamiquement le texte du TextView, je l'ai déjà essayé simplement d'appeler le TextView et modifier le texte, mais il ne fonctionne pas, donc j'espère que vous pouvez me aider

Ceci est mon code

LayoutInflater inflater = getLayoutInflater(); 
    View layout = inflater.inflate(
      R.layout.elemento_correcto_xl 
      , (ViewGroup) findViewById(R.id.elemento_correcto_xl)); 

    TextView tvCombo = (TextView) findViewById(R.id.tv); 
    if(combo > 1) { 
     tvCombo.setText("¡" + combo + " VECES SEGUIDAS!"); 
    } 
    else 
     tvCombo.setText("¡CORRECTO!"); 

    this.elementoCorrecto = new Toast(this); 
    this.elementoCorrecto.setGravity(Gravity.TOP, 0, 0); 
    this.elementoCorrecto.setDuration(Toast.LENGTH_LONG); 
    this.elementoCorrecto.setView(layout); 
    this.elementoCorrecto.show(); 

Répondre

0

Vous pouvez utiliser cette méthode pour que je passe la chaîne à afficher ..

public static void makeToast(Context c, String msgToShow){ 
    LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v= inflater.inflate(R.layout.view_toast,null); //your layout to show 
    TextView text = (TextView) v.findViewById(R.id.textViewToast); 
    text.setText(msgToShow); 
    Toast toast = new Toast(c); 
    toast.setGravity(Gravity.BOTTOM, 10, 25); 
    toast.setDuration(Toast.LENGTH_LONG); 
    toast.setView(v); 
    toast.show(); 
} 

Appelez où vous voulez dans les activités.

+0

Un homme génial! tu as vraiment compris mon problème, j'ai essayé et ça marche! Merci –

0

Y a-t-il une raison particulière d'afficher un toast en utilisant TextView? Sinon, vous pouvez suivre ci-dessous méthode pour afficher dynamiquement des toasts -

String toastText = ""; 
if(combo > 1) { 
    toastText = "¡" + combo + " VECES SEGUIDAS!"; 
} 
else 
    toastText = "¡CORRECTO!"; 
Toast.makeText(activity, toastText, Toast.LENGTH_SHORT).show();