2011-08-11 13 views
20

私は自分のすべてのアクティビティのカスタムテーマを作成しました。テーマでは、アンドロイド:背景を設定しています。これにより、ダイアログやトーストメッセージが非常に奇妙に見えるようになります。トーストの背景がアクティビティのテーマに一致するように変更する

トーストと他のダイアログがテーマのプロパティを吸収しないようにするにはどうすればよいですか?

答えて

51

あなたは簡単に次のコードによって、カスタムトーストを作成することができます

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

それとも、完全なカスタムトーストをインスタンス化することができます

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

ダイアログのカスタマイズは、より複雑なルーチンです。しかし同様の回避策があります。

+3

Javacadabraの答えは、私の意見では優れている – rubdottocom

+1

私は間違った質問を読んでいますか?しかし、この質問は、カスタマイズすることを防ぐ方法を尋ねています。あなたはカスタマイズする方法を教えていますか? – WORMSS

+0

@WORRMS、あなたはそうですが...テーマが変更されている限り、このテーマを適用しないトーストはカスタムトーストです(それを元に戻す必要があるため) – Dmitry

4

ここでは完全な例があり、カスタマイズされたトーストのアクティビティに使用されます。

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

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

私は質問が回答されている実感とポストは、この段階ではかなり古いです。しかし、私はこの質問に遭遇した人たちに答えを残すと思った。

私は今日、この問題でのトラブルに走ったと私は解決の方法は、それがこのように私のトーストメッセージ表示していた:(メッセージを想定すると、ビュー内から呼び出されている)、これに対し

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

を:

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

問題が解決しました。とにかくそれが助けて欲しい。ここに似たような話題の私の質問へのリンクです。

Toast background color being changed

+1

super。ありがとう!! – OWADVL

+1

素晴らしいコメント、ありがとうございます! btw、最初の&受け入れられた答えは私のために働かなかったが、あなたの解決策はありません。 – middlehut

+1

ありがとう、私はOPと同じ質問をして、これは私のためにうまくいった! – deanresin

関連する問題