2011-10-03 43 views
2

此刻,我擴展Dialog並使用構造函數Dialog(Context context, int theme)通過主題設置背景顏色。該對話框是一個疊加層,因此它位於屏幕上顯示的所有內容之上。主題如下:可以更改沒有主題的對話框的窗口背景顏色?

<style 
    name="Theme.example" 
    parent="android:Theme"> 
    <item name="android:windowIsTranslucent">true</item> 
    <item name="android:backgroundDimEnabled">false</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowIsFloating">false</item> 
    <item name="android:windowFullscreen">false</item> 
    <item name="android:windowAnimationStyle">@null</item> 
    <item name="android:windowFrame">@null</item> 
    <item name="android:windowBackground">@color/background_color</item> 
</style> 

請注意,此主題通過android:windowBackground屬性設置背景顏色。我需要整個屏幕更改顏色,包括通知欄。但我也希望能夠在顯示對話框後以Java動態更改背景。我想出的最好方法是使用getWindow().setBackgroundDrawableResource(R.drawable.background),其中Drawable只是我想要的顏色的單個像素。

有沒有更好的方法來做到這一點?我目前的方法工作正常,但能夠使用我沒有在R.drawable中預定義的顏色會很好。

回答

3

嘗試使用ColorDrawable類。

您可以創建ColorDrawable類的新實例並將其設置爲背景。無論何時您需要更改顏色,您都可以致電該類別的setColor(int color)

ColorDrawable colorDrawable = new ColorDrawable(); 
// ColorDrawable colorDrawable = 
//  new ColorDrawable(0xFF00FF00); // With a custom default color. 

colorDrawable.setColor(0xFFFF0000); 
+0

乾杯!效果很好。 :) – Glitch