I'm creating an app for use by pilots. It mostly uses TextView
, EditText
, Button
, etc. In other words, not very graphics-heavy.
When used at night, I'd like for the user to be able to switch to a "night mode" where everything on screen is red and 开发者_如何学运维black... like the Google Sky Map app. (This is important for pilots because the color red does not destroy the eye's natural night vision adaptation as other colors do.)
What's the best way to do this? I found APIs like ColorFilter
, etc. but I'm not sure how I'd apply these app-wide.
If you're simply talking about changing the colours of the various views within the layout and not the structure of the layout itself, then I'd suggest you simply do this using styles.
i.e. Define two sets of styles for the various views which make up your layout - one set for night mode and one for day mode. In your activity, it then becomes a case of calling setStyle()
on each of your views to toggle it between night and day.
Coming up with a sensible naming convention and parentage scheme for your styles will make your life a lot easier. e.g. MyText.Large
vs MyText.Large.Night
.
The Android developer doc on Themes and Style is a good starting point.
Creating a day and night theme and setting it programmatically would work. But it also requires you to duplicate your styles, just to specify different drawables/colors.
Instead, you can maintain one style and use Android's built in day/night mode support. Just as you can have multiple variations of a single layout (for landscape or portrait screen orientations,) drawable (for different pixel densities) and string (for different locales,) you can also have day and night versions of your drawables and colors. The specifiers are "-night" and "-notnight" but you will probably want to use "-night" or nothing, making your day version the default. For example:
res/drawable/fancy-button-background.9.png
res/drawable-night/fancy-button-background.9.png
Android should be able to switch the day/night mode automatically but the details on how and when that happens are a little unclear to me. To change the mode yourself, you can use UiModeManager#setNightMode()
. Just make sure car mode is enabled first, using enableCarMode(0)
.
精彩评论