Implementation code example of Android night mode
Night mode implementation
The so-called night mode is that it can present different styles of interfaces to users according to different settings, and it doesn't hurt your eyes at night. The implementation method is the so-called skin change (theme switching). There are many ways to realize the night mode on the Internet. It also decompiled several news classes (you know) and implemented better apps in the night mode. At least it was implemented. There are many ways. I now share the principles I have implemented (the way of built-in theme) and hope to help you. Don't spray if you don't like it (recently, I'm careful about my liver). If there are better methods, please share them.
When implementing night mode, I have been struggling with the following problems
Let's solve the above problems one by one:
First: where to start
1.1 defining attributes
To set different properties according to different topics, we need to at least define the name of the property. Otherwise, how can the system know where to find it!
Define attributes under values.
Several attributes are defined in attrs.xml.
As can be seen from the above XML file, various attribute types can be defined in attr, such as color, float, integer, Boolean, dimension (SP, DP / dip, PX, Pt...), reference (pointing to local resources), and curvisibility is an enumeration attribute, corresponding to the invisibility, visibility and gone of view.
1.2 defining topics
Next, we need to define several sets of topics in the resource file. And set the value of each attribute in the topic.
In this example, I defined daytheme and nighttheme in styles.xml.
1.3 use in layout documents
After defining the attributes, we will use them in the layout file.
In order to use the properties in the topic to configure the interface, I defined a layout file called setting. XML.
From this layout file, you can see that the values in the theme are referenced in the format of "? Attr /...", including string, picture, bool type, size setting, etc.
1.4 setting theme and layout documents
After the layout file and theme have been written, we will use it in the oncreate method of activity.
It should look like this:
ps:
If you use a fragment, it should look like the following:
PS: it is recommended to put it in the oncreateview (...) method.
It should be noted that if there are no properties in the default theme, it will crash when parsing the layout file. In this regard, when configuring multiple themes with different styles, you can have more attributes, but you must not have less.
For example, in the attrs.xml file
These two properties are not used, but there is no problem.
If you follow the above operation, you should see the effect when the program runs
What about the second question?
Look directly at the source code
Source address: nightmodel_ jb51.rar
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.