2011-07-14 9 views
46

estoy mostrando un mensaje de aviso emergente como el resultado de una sentencia if usando el siguiente código:Android: Cómo modificar el color del texto de un pan tostado

Toast.makeText(getBaseContext(), "Please Enter Price", Toast.LENGTH_SHORT).show(); 

Se muestra como texto blanco sobre un fondo blanco, como tal, no se puede leer! Mi pregunta es, ¿cómo puedo cambiar el color del texto de la tostada?

+0

I Hope [esto] (http://linkflows.blogspot.in/2014/08/creating-custom-toast-using-xml.html) le ayudará a . [Ver este enlace.] (Http://linkflows.blogspot.in/2014/08/creating-custom-toast-using-xml.html) –

Respuesta

15

Es posible que desee crear un pan tostado encargo

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

-

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

ImageView image = (ImageView) layout.findViewById(R.id.image); 
image.setImageResource(R.drawable.android); 
TextView text = (TextView) layout.findViewById(R.id.text); 
text.setText("Hello! 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(); 

Source

+0

Muchas gracias –

114

se puede lograr esto muy fácilmente, sin crear un diseño personalizado al modificar el Toast predeterminado:

Toast toast = Toast.makeText(this, resId, Toast.LENGTH_SHORT); 
TextView v = (TextView) toast.getView().findViewById(android.R.id.message); 
v.setTextColor(Color.RED); 
toast.show(); 

se puede encontrar el modelo usado por la vista por defecto tostadas en el SDK de Android:

$ ANDROID-SDK $/plataformas/android-8/datos/res/layout/transient_notification.xml

+56

También puede hacer 'tostar.obtener() .setBackgroundColor (Color.RED); 'para establecer el color de fondo de toda el área de Toast. – Chris

+0

^- En mi teléfono que agrega un fondo detrás del fondo gris predeterminado, sin embargo. –

5

También puede usar SpannableString. También puede colorear partes de la cuerda.

SpannableString spannableString = new SpannableString("This is red text"); 
spannableString.setSpan(
          new ForegroundColorSpan(getResources().getColor(android.R.color.holo_red_light)), 
          0, 
          spannableString.length(), 
          0); 
Toast.makeText(this, spannableString, Toast.LENGTH_SHORT).show(); 
+0

por encima del valor único ** El método getColor (int) ** está en desuso, por lo que es mejor utilizar el método de doble valor como ** getColor (int, theme) **. EX :: ** getColor (android.R.color.holo_red_light, getTheme()) ** –

6

La forma más sencilla de cambiar el color de fondo de un pan tostado y el color de fondo del texto de un brindis:

View view; 
TextView text; 
Toast toast; 
toast.makeText(this, resId, Toast.LENGTH_SHORT); 
view = toast.getView(); 
text = (TextView) view.findViewById(android.R.id.message); 
text.setTextColor(getResources().getColor(R.color.black)); 
text.setShadowLayer(0,0,0,0); 
view.setBackgroundResource(R.color.white); 
toast.show(); 
Cuestiones relacionadas