2016-12-07 3 views
0

Ich habe ein Problem meine App in Android Studio erstellen, erkläre ich michDynamisches Layout eines Android Toast ändern

ich ein Layout für meinen Toast erstellt, die XML-ähnliche

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

sieht

Es funktioniert gut, wenn ich das Layout

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

Aber das Problem ist aufblasen , die ich ändern will, habe ich versucht, dynamisch den Text für die Textview, schon nur die Textview-Aufruf und den Text zu ändern, aber es funktioniert nicht, so dass ich hoffe, dass Sie mir

helfen können

Das ist mein Code ist

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

Antwort

0

Sie können diese Methode verwenden, wie ich den String übergeben zu zeigen ..

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

Nennen Sie es, wo immer Sie in den Aktivitäten wollen.

+0

Ehrfürchtiger Mann! Du hast mein Problem wirklich verstanden, ich habe es versucht und es funktioniert! Vielen Dank –

0

Gibt es einen besonderen Grund, einen Toast mit TextView anzuzeigen? Ansonsten können Sie unter Verfahren folgen Toast zu zeigen, dynamisch -

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