2011-07-14 33 views
46

Tôi đang hiển thị một thông điệp chúc mừng là kết quả của một của một Toast lệnh if sử dụng đoạn mã sau:Android: Làm thế nào để thiết lập màu sắc của văn bản

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

Nó được hiển thị dưới dạng văn bản màu trắng trên nền trắng, như vậy nó không thể được đọc! Câu hỏi của tôi là, làm thế nào tôi có thể thay đổi màu sắc của văn bản của bánh mì nướng?

+0

I Hope [này] (http://linkflows.blogspot.in/2014/08/creating-custom-toast-using-xml.html) sẽ giúp bạn . [Kiểm tra liên kết này.] (Http://linkflows.blogspot.in/2014/08/creating-custom-toast-using-xml.html) –

Trả lời

15

Bạn có thể muốn tạo ra một Toast tùy chỉnh

<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

Cảm ơn bạn rất nhiều –

114

Bạn có thể đạt được điều này rất dễ dàng, mà không cần tạo bố cục tùy chỉnh bằng cách sửa đổi Bánh mì nướng mặc định:

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

Bạn có thể tìm ra cách bố trí sử dụng bởi các quan điểm bánh mì nướng mặc định trong Android SDK:

$ ANDROID-SDK $/nền tảng/android-8/data/res/layout/transient_notification.xml

+56

Bạn cũng có thể làm 'toast.getView() .setBackgroundColor (Color.RED); 'để thiết lập màu nền của toàn bộ vùng Toast. – Chris

+0

^- Tuy nhiên, trên điện thoại của tôi có thêm nền phía sau nền màu xám mặc định. –

5

Bạn cũng có thể sử dụng SpannableString. Nó cũng có thể tô màu các phần của chuỗi.

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

ở trên giá trị đơn ** phương thức getColor (int) ** không được chấp nhận vì vậy tốt hơn nên sử dụng phương thức giá trị kép như ** getColor (int, chủ đề) **. EX :: ** getColor (android.R.color.holo_red_light, getTheme()) ** –

6

Cách đơn giản nhất để thay đổi màu nền của một bánh mì nướng và màu nền của văn bản của bánh mì nướng:

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(); 
Các vấn đề liên quan