2016-04-26 15 views
2

Wie kann ich die Farbe der Nachricht Toast ändern?Ändern Sie die Farbe von Toast

Hier mein Code:

public void checkButton(View view) { 


    if(count < 0){ 
     Toast.makeText(getApplicationContext(), "Incorreto!", 
       Toast.LENGTH_SHORT).show(); 
    } 

    else if(count == 0){ 
     Toast.makeText(getApplicationContext(), "Correto", 
       Toast.LENGTH_SHORT).show(); 
     } 

    } 
} 
+0

http://stackoverflow.com/questions/6687666/android- how-to-set-the-Farbe-von-Toasts-Text –

Antwort

7
Toast toast = Toast.makeText(getApplicationContext(), "Correto!", 
       Toast.LENGTH_SHORT); 

TextView toastMessage = (TextView) toast.getView().findViewById(android.R.id.message); 
toastMessage.setTextColor(Color.RED); 
toast.show(); 
+0

Oder verwenden Sie ein eigenes Layout für einen Toast. –

+0

Bogdan Ustyak! Ich danke dir sehr! –

2

Erstellen Sie eine benutzerdefinierte Toast Layout, wie correct_toast.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/toast_layout_root" 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="8dp" 
      android:background="#DAAA"> 
    <TextView android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textColor="#FFF" 
       /> 
</LinearLayout> 

Dann in der Java-Code, konstruieren den Toast mit dieser Ansicht:

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

TextView text = (TextView) layout.findViewById(R.id.text); 
text.setText("This is a custom toast"); 

Toast toast = new Toast(getApplicationContext()); 
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); 
toast.setDuration(Toast.LENGTH_LONG); 
toast.setView(layout); 
toast.show(); 

Auf diese Weise können Sie die Farbe des Hintergrunds ändern und/oder die Farbe des Textes ändern.

1

Verfahren die Farbe, die Position und die Hintergrundfarbe des Toasts zu ändern ist:

Toast toast=Toast.makeText(getApplicationContext(),"This is advanced toast",Toast.LENGTH_LONG); 
    toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT,0,0); 
    View view=toast.getView(); 
    TextView view1=(TextView)view.findViewById(android.R.id.message); 
    view1.setTextColor(Color.YELLOW); 
    view.setBackgroundResource(R.color.colorPrimary); 
    toast.show(); 

Für zeilen Erläuterung: https://www.youtube.com/watch?v=5bzhGd1HZOc

Verwandte Themen