2013-01-09 38 views
8

我工作的一個項目,並設置應用程序的背景爲白色如下:吐司背景顏色做被改變

<!-- Application theme. --> 
<style name="AppTheme" parent="AppBaseTheme"> 
    <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
    <item name="android:actionBarStyle">@style/MyActionBar</item> 
    <item name="android:actionBarSize">140dp</item> 
    <item name="android:background">#ffffff</item> 
</style> 

這工作然而魅力的問題是,敬酒的消息現在被顯示有白色背景。奇怪的是,我將啓動畫面集成到項目中,並且用戶登錄Toast消息時正常顯示。

這真的很奇怪,希望對這個問題有任何幫助。

編輯:添加屏幕快照顯示問題。該截圖是採取只作爲初始吐司(與不必要的效應)淡出,而新的(默認)的衰落。

enter image description here

回答

14

我解決了問題。 Toast背景顏色發生變化的原因是由於我傳遞的View對象的上下文中包含了它。

下面的代碼行將使背景顏色更改爲不需要的白色:

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

這行代碼將返回舉杯系統默認樣式:

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

我不確定這樣修復是否存在一個巨大的問題,因爲我只是在學習。如果有人可以看到問題,請分享。它似乎工作得很好,但。

+1

這是非常有益的 – FtheBuilder

0

可以使用視圖可以輕鬆地定製和時尚敬酒消息。也許改變你的佈局toast_layout。

檢查這個帖子Toast background changing to match Activity's Theme,它會幫助你

+0

我實際上遇到過這種情況。真正的事情,我不明白爲什麼我的一些敬酒有不同的風格給其他人... – Javacadabra

+0

未來的問題,這將是更好的發佈更多的代碼,有一個完整的問題的上下文。無論如何,祝賀你解決它 –

0

對我來說,使用getApplicationContext()不是一種選擇,而對於其他人有同樣的問題,你可以設置敬酒回默認設置,如下所示:

//Create your Toast with whatever params you need 
Toast toast = Toast.makeText(getActivity(), "Refreshing...", Toast.LENGTH_SHORT); 

//Set the background for the toast using android's default toast_frame. 
//Optionally you can set the background color to #646464 which is the 
//color of the frame 
View view = toast.getView(); 
view.setBackgroundResource(android.R.drawable.toast_frame); 

//Get the TextView for the toast message so you can customize 
TextView toastMessage = (TextView) view.findViewById(android.R.id.message); 

//Set background color for the text. 
toastMessage.setBackgroundColor((Color.parseColor("#646464"))); 
toast.show(); 
0

的除了溫思羅普的答案。不是將文本框的背景顏色設置爲#646464,而是將其設置爲透明,以使麪包看起來像原始半透明烤麪包。

private void showToast(Context context,String msg,int duration){ 
     Toast toast = Toast.makeText(context,msg,duration); 

     View view = toast.getView(); 
     view.setBackgroundResource(android.R.drawable.toast_frame); 

     TextView toastMessage = (TextView) view.findViewById(android.R.id.message); 

     toastMessage.setBackgroundColor(Color.TRANSPARENT); 

     toast.show(); 
    }