2011-07-14 13 views
46

jestem wyświetlając komunikat tost w wyniku instrukcji if stosując następujący kod:Android: Jak ustawić kolor tekstu toast za

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

jest wyświetlany jako biały tekst na białym tle, jako taki nie można go odczytać! Moje pytanie brzmi: jak mogę zmienić kolor tekstu tosty?

+0

I Hope [to] (http://linkflows.blogspot.in/2014/08/creating-custom-toast-using-xml.html) pomoże . [Sprawdź ten link.] (Http://linkflows.blogspot.in/2014/08/creating-custom-toast-using-xml.html) –

Odpowiedz

15

Możesz utworzyć własny tost

<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

Dziękuję bardzo –

114

Można to osiągnąć bardzo łatwo, bez tworzenia niestandardowego układu, modyfikując domyślną toast:

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

można znaleźć układ używany przez widok domyślny tosty w Android SDK:

$ Android SDK $/platform/android-8/danych/RES/Układ/transient_notification.xml

+56

Możesz także zrobić toast.getView() .setBackgroundColor (Color.RED); ', aby ustawić kolor tła całego obszaru Toast. – Chris

+0

^- Na moim telefonie, który dodaje tło za domyślnym szarym tłem. –

5

Można również użyć SpannableString. Może również zabarwiać części struny.

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

powyżej pojedynczej wartości ** getColor (int) ** metoda jest przestarzała, więc lepiej użyć metody podwójnej wartości, np. ** getColor (int, theme) **. EX :: ** getColor (android.R.color.holo_red_light, getTheme()) ** –

6

Najprostszym sposobem, aby zmienić kolor tła tosty i kolor tła tekstu toast za:

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(); 
Powiązane problemy