Possible to change Window background colour of a Dialog without a theme?
At the moment I extend a Dialog
and use the constructor Dialog(Context context, int theme)
to set the background colour via a theme. This dialog is an overlay, so it sits above everything showing on the screen. The theme is as follows:
<style
name="Theme.example"
parent="android:Theme">
<item name="android:windowIsTra开发者_如何学运维nslucent">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>
Note that this theme sets the background colour via the android:windowBackground
attribute. I need the entire screen to change colour, including the notification bar. But I also want to be able to change the background dynamically in Java after the dialog is displayed. The best way I've come up with is using getWindow().setBackgroundDrawableResource(R.drawable.background)
, where the Drawable
is just a single pixel of the colour I want.
Is there a better way to do this? My current method works fine, but it would be nice to be able to use colours that I haven't predefined in R.drawable
.
Try it with the ColorDrawable
class.
You can create a new instance of the ColorDrawable
class and set it as the background. Whenever you need to change the color you can just call setColor(int color)
on that class.
ColorDrawable colorDrawable = new ColorDrawable();
// ColorDrawable colorDrawable =
// new ColorDrawable(0xFF00FF00); // With a custom default color.
colorDrawable.setColor(0xFFFF0000);
精彩评论