2016-04-26 2 views
2

Как я могу изменить цвет сообщения Toast?Изменить цвет Toast

Вот мой код:

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- как-to-set-the-color-of-a-toasts-text –

ответ

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

Или используйте собственный макет для тоста. –

+0

Богдан Устяк! Спасибо огромное! –

2

Создание макета пользовательского Toast, например 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> 

Затем в коде Java, построить тост с этой точки зрения:

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

Таким образом, вы можете изменить цвет фона и/или изменить цвет текста.

1

Способ изменить цвет, положение и цвет фона тоста является:

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

Для построчной объяснения: https://www.youtube.com/watch?v=5bzhGd1HZOc

Смежные вопросы