2013-04-16 7 views
2

멀티 컬러 토스트가 필요합니다. 이처럼 :안드로이드 토스트 멀티 컬러

Multi Color Toast

나는 사용자 정의 토스트를 만들기 위해 XML로 레이아웃을 변경하는 방법에 대한 다른 자습서 보았다, 그러나 그들 중 누구도이 같은 다른 색상을 추가 설명이 없습니다.

어떻게하면됩니까?

================================

답변

=== ===============

모든 도움을 사용하여 색상 토스트를 만드는 간단한 방법()을 고안했습니다. 전화하기가 더 쉽다.

고해상도/레이아웃/toast_layout.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:paddingLeft="12dp" 
    android:paddingRight="12dp" 
    android:paddingBottom="6dp" 
    android:paddingTop="6dp" 
    android:background="#DAAA" 
    > 
    <TextView android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 
</LinearLayout> 

SRC/PROJECTNAME/FILENAME.java

// Color Toast(String1,String2,Color) 
// Toastbackground = White 
// String1 = Dark Gray 
// String2 - **CASE SENSITIVE** 
// = "same" = Dark Gray, or 
// = "purple" = Purple, or 
// = "orange" = Orange 

    public void CToast(String t1, String t2, String c) { 
     if (c == "same") { 
      c = "444444"; 
     } else if (c == "purple") { 
      c = "6600FF"; 
     } else if (c == "orange") { 
      c = "ffcc00"; 
     } 

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

     String text2 = "<font color=#444444>" + t1 + "</font> <font color=#" + c + ">" + t2 + "</font"; 
     textCToast.setText(Html.fromHtml(text2)); 

     Toast toast = new Toast(this); 
     toast.setDuration(Toast.LENGTH_SHORT); 
     toast.setView(layout); 
     toast.show(); 
    } 

희망이 도움이! 모두에게 감사합니다

답변

6

yo0ur 사용자 정의 레이아웃에서 textView를 배치하십시오. 이

String text = "This is <font color='red'>red</font>. This is <font color='blue'>blue</font>."; 
textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE); 

N.B은 .. 예 나는 당신이 SpannableText 건배한다고 생각 here

2

에서 촬영 된 모든 사진 - 사용하여 해당 사용자가 색상, 스타일을 적용하고 문자열로 스마일 등을 삽입 할 수 있습니다.

그래서, 어떻게 든 시도하고 해결하는 나의 첫 번째 아이디어가 될 것입니다.

필자는 텍스트 및 다중 색상 알림을 그림으로 그려야한다는 것을 알고 있습니다. 아니면 그냥 spannable 필터를 적용해야합니다. 참조 here

3

대신의 setText이이

Toast.makeText(context, text, Toast.LENGTH_LONG).show(); 

처럼 사용하려고 내가이 당신이 realyy

richTextView = (TextView)findViewById(R.id.rich_text); 

    // this is the text we'll be operating on 
    SpannableString text = new SpannableString("hello how are you"); 

    // make "hello" to (characters 0 to 5) red color 
    text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0); 

richTextView.setText(text, BufferType.SPANNABLE); 

원하는 희망 그리고 당신이 원하는 경우는 토스트로 보여주기 위해이 일을보십시오 enter image description here

+0

이것은 제가 제안한 것입니다 만, copypastable 코드를 가지고 있습니다 : +1 – Shark