2016-12-07 82 views
0

我有麻煩創建我的Android Studio中的應用程序敬酒的佈局,我解釋一下動態修改的Android

我創建了一個佈局爲我敬酒時,XML看起來像這樣

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

當我吹佈局

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

它工作正常,但問題是,我想動態改變文本TextView的,我已經嘗試過只調用TextView的和不斷變化的文本,但它不工作,所以我希望你能幫助我

這是我的代碼

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

回答

0

您可以使用此方法,因爲我傳遞字符串顯示..

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

調用它,無論你在活動希望。

+0

太棒了!你真的明白我的問題,我試過了,它的工作原理!謝謝 –

0

,有什麼特別的理由表明使用TextView的敬酒?否則,你可以按照下面的方法來動態顯示敬酒 -

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