2011-08-11 7 views

Respuesta

51

Puede crear fácilmente tostadas a medida por el siguiente código:

Toast toast = Toast.makeText(context, resTxtId, Toast.LENGTH_LONG); 
View view = toast.getView(); 
view.setBackgroundResource(R.drawable.custom_bkg); 
TextView text = (TextView) view.findViewById(android.R.id.message); 
/*here you can do anything with text*/ 
toast.show(); 

O puede crear una instancia de un brindis completamente personalizado:

Toast toast = new Toast(context); 
toast.setDuration(Toast.LENGTH_LONG); 

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View view = inflater.inflate(R.layout.custom_layout, null); 
toast.setView(view); 
toast.show(); 

personalización de diálogo es una rutina más compleja. Pero hay una solución similar.

+3

Javacadabra respuesta es mejor en mi opinión – rubdottocom

+1

Estoy leyendo la pregunta equivocada? Pero la pregunta es ¿CÓMO PREVENIR que se personalice, y usted está indicando CÓMO personalizarlo? – WORMSS

+0

@WORRMS, tienes razón, pero ... En cuanto al tema se cambia, cualquier brindis que no aplique este tema es un brindis personalizado (porque tenemos que "volver a darle estilo") – Dmitry

4

aquí viene el ejemplo completo, que se utilizará para las actividades personalizadas de Toast across.

displayToast

// display customized Toast message 
    public static int SHORT_TOAST = 0; 
    public static int LONG_TOAST = 1; 
    public static void displayToast(Context caller, String toastMsg, int toastType){ 

     try {// try-catch to avoid stupid app crashes 
      LayoutInflater inflater = LayoutInflater.from(caller); 

      View mainLayout = inflater.inflate(R.layout.toast_layout, null); 
      View rootLayout = mainLayout.findViewById(R.id.toast_layout_root); 

      ImageView image = (ImageView) mainLayout.findViewById(R.id.image); 
      image.setImageResource(R.drawable.img_icon_notification); 
      TextView text = (TextView) mainLayout.findViewById(R.id.text); 
      text.setText(toastMsg); 

      Toast toast = new Toast(caller); 
      //toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); 
      toast.setGravity(Gravity.BOTTOM, 0, 0); 
      if (toastType==SHORT_TOAST)//(isShort) 
       toast.setDuration(Toast.LENGTH_SHORT); 
      else 
       toast.setDuration(Toast.LENGTH_LONG); 
      toast.setView(rootLayout); 
      toast.show(); 
     } 
     catch(Exception ex) {// to avoid stupid app crashes 
      Log.w(TAG, ex.toString()); 
     } 
    } 

y toast_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<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="10dp" 
       android:background="#DAAA" 
       > 
    <ImageView android:id="@+id/image" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_marginRight="10dp" 
       /> 
    <TextView android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:textColor="#FFF" 
       /> 
</LinearLayout> 
25

realizo la pregunta ha sido contestada y el puesto es bastante antiguo en esta etapa. Sin embargo, pensé que dejaría una respuesta para aquellos que se encuentran con esta pregunta.

que tuvo problemas con este tema hoy y la forma en que decidí que era exhibiendo mis mensajes Toast así:

Toast.makeText(getApplicationContext(), "Checking login details...", Toast.LENGTH_SHORT).show(); 

En oposición a esto (suponiendo que el mensaje está siendo llamado desde dentro de una vista) :

Toast.makeText(v.getContext(), "Checking login details...", Toast.LENGTH_SHORT).show(); 

Aclaró los problemas que estaba teniendo. De todos modos espero que ayude. Aquí hay un enlace a mi pregunta sobre un tema similar.

Toast background color being changed

+1

super. ¡¡Gracias!! – OWADVL

+1

gran comentario, gracias! Por cierto, la primera y aceptada respuesta no funcionó para mí, pero su solución sí. – middlehut

+1

¡Gracias, tuve la misma pregunta que el OP y esto funcionó para mí genial! – deanresin

Cuestiones relacionadas